Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 760261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:41:45+00:00 2026-05-14T15:41:45+00:00

I have a c++ project being developed in QT. The problem I’m running in

  • 0

I have a c++ project being developed in QT. The problem I’m running in to is I am wanting to have a single base class that all my property classes inherit from so that I can store them all together. Right now I have:

class AbstractProperty
        {
        public:
            AbstractProperty(QString propertyName);
            virtual QString toString() const = 0;
            virtual QString getName() = 0;
            virtual void fromString(QString str) = 0;
            virtual int toInteger() = 0;
            virtual bool operator==(const AbstractProperty &rightHand) = 0;
            virtual bool operator!=(const AbstractProperty &rightHand) = 0;
            virtual bool operator<(const AbstractProperty &rightHand) = 0;
            virtual bool operator>(const AbstractProperty &rightHand) = 0;
            virtual bool operator>=(const AbstractProperty &rightHand) = 0;
            virtual bool operator<=(const AbstractProperty &rightHand) = 0;
        protected:
            QString name;
        };

then I am implementing classes such as PropertyFloat and PropertyString and providing implementation for the comparator operators based on the assumption that only strings are being compared with strings and so on. However the problem with this is there would be no compiletime error thrown if i did

 if(propertyfloat a < propertystring b)

however my implementation of the operators for each derived class relies on them both being the same derived class. So my problem is I cant figure out how to implement a property structure so that I can have them all inherit from some base type but code like what I have above would throw a compile time error.

Any ideas on how this can be done? For those familiar with QT I tried using also a implementation with QVariant however QVariant doesn’t have operators < and > defined in itself only in some of its derived classes so it didn’t work out.

What my end goal is, is to be able to generically refer to properties. I have an element class that holds a hashmap of properties with string ‘name’ as key and the AbstractProperty as value. I want to be able to generically operate on the properties. i.e. if I want to get the max and min values of a property given its string name I have methods that are completely generic that will pull out the associated AbstactProperty from each element and find the max/min no matter what the type is. so properties although initially declared as PropertyFloat/PropertyString they will be held generically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:41:45+00:00Added an answer on May 14, 2026 at 3:41 pm

    Another solution is to use the Curiously Recurring Template Pattern. This allows a templated class to define comparison operators based on a derived class.

    Example:

    template <class Descendant>
    struct Numeric_Field
    {
        Descendant    m_value;
    
        bool   operator==(const Descendant& d)
        {
            return m_value == d.value;
        }
        bool   operator!=(const Descendant& d)
        {
            return !(*this == d);
        }
        bool   operator< (const Descendant& d)
        {
            return m_value < d.m_value;
        }
        bool   operator<=(const Descendant& d)
        {
            return (*this < d) || (*this == d);
        }
        bool   operator> (const Descendant& d)
        {
            return !(*this <= d);
        }
        bool   operator>=(const Descendant& d)
        {
            return !(*this < d);
        }
    
    protected:
        Numeric_Field(const Descendant& new_value = 0)
        :  m_value(new_value)
        { ;}
    };
    

    This could be made a little more generic by replacing m_value by using pure virtual protected setters and getters:

    template <class Descendant_Type>
    struct Numeric_Field_2
    {
        virtual const Descendant_Type    get_value(void) const = 0;
        virtual void                     set_value(const Descendant& new_value) = 0;
    
        bool operator==(const Descendant_Type& dt)
        {
            return get_value() == dt.get_value();
        }
        bool operator< (const Descendant_Type& dt)
        {
            return get_value() < dt.get_value();
        }
    };
    

    At this point, you could pass around pointers to Numeric_Field wherever a comparison is needed. I believe this is only a typing saver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.