Torsten
Torsten

Reputation: 24166

Overriding class property via class declaration

Question is not easy to describe by several words in title. So, i've a some manager who managed of some type objects implement by user. Object can be 2 types, for example - positive and negative. So i want to get user set this options via class declaration. Obvious way:

class Base;
class BasePositive : Base;
class BaseNegative : Base;

class UserObject1 : public BasePositive;
class UserObject2 : public BaseNegative;

Fine, it's worked well. But if we want to use inheritance:

class UserObjectBase : public BasePositive;
class UserObject3 : public UserObjectBase;

Ops, all derived classes from UserObjectBase can be only Positive. I want to get user reliable mechanism to declare own classes and set type of positive or negative (for example). So, i've search some mechanism to override some field/property in base class (no matter what it is - just achieve it).

Really, i didn't see any solution without macros. But may be somebody else known?

C++11 - extensions can't be used (i want, but can't).

UPDATE:

With virtual inheritance i've some success:

#include <iostream>

struct base {
    ~base() {}
    int value;

    bool isPositive;    
};

struct pos : virtual base {
    pos() {
        isPositive = true;
    }
};

struct neg : virtual base {
    neg() {
        isPositive = false;
    }
};

struct foobase : pos {};
struct fooNeg :  virtual foobase, virtual neg {};
struct fooPos : foobase {}; 

int main(int argc, char const *argv[])
{ 
    fooNeg f;
    std::cout << f.isPositive << "\n";
    fooPos p;
    std::cout << p.isPositive << "\n";
    return 0;
}

But virtual inheritance is bad practice as i known. Looking something else ...

Upvotes: 0

Views: 798

Answers (3)

xtofl
xtofl

Reputation: 41509

Could it be that you want to express a 'has-a' relationship with an 'is-a' technique?

At first sight, it seems that all user classes 'have-a' polarity. Expressing this attribute through (single) inheritance is awkward, and indeed leeds to the problem you have: the user has to choose whether a class contains positive or negative values, although his subclasses could be both.

Multiple inheritance as suggested by Joachim could be a technique to achieve a succinct way to express the polarity of a user class, but most of the time I used it, it brings more bad than good. Do read Meyers' article on that; at least, make it private!

Maybe inheritance is just not a clean solution for this problem. Pmr's hint also points in that direction. Imagin the scalability issues arising when you add another discriminating attribute e.g. discrete/continuous, high/low, ...

Your client's code may become more verbose than you would like, but at least, you aren't forced into a class explosion!

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409176

Remember that C++ can handle multiple inheritance, so you could use separate base classes and combine them. Something like:

class PosNegBase;
class Positive : public PosNegBase;
class Negative : public PosNegBase;

class UserObjectBase;
class UserObject3 : public UserObjectBase, public Positive;
class UserObject4 : public UserObjectBase, public Negative;

Upvotes: 0

pmr
pmr

Reputation: 59811

Could it be you are looking for pure virtual function?

class Base {
public:
  enum Polarity { POSITIVE, NEGATIVE };
  virtual Pole polarity() const = 0;
};

This forces users to declare if their class is either positive or negative and would still leave a choice further down the hierarchy.

Upvotes: 1

Related Questions