1

For easy RTTI I thought about initializing a protected base class member variable using an enum.

class myBase
{
public:
    typedef enum class mySubType { base, type1 };
    myBase() : myType( mySubType::base ) {}
    virtual ~myBase();
protected:
    mySubType myType;
};

Now, to initialize this inside a derived class, can I write the derived CTor like this:

myDerived::myDerived() : myBase(), myType( mySubType::type1 ) {}

???

pi84
  • 21
  • 3
  • Do you expect that `protected` will make it work when `public` doesn't? – juanchopanza Jun 30 '16 at 06:30
  • 1
    How do you get that idea? I made it protected, so a slight chance exists it would work. Else I'd have made it private. – pi84 Jun 30 '16 at 06:35
  • The question is why you think that if it doesn't work with `public`, somehow it will work with `protected`? – juanchopanza Jun 30 '16 at 06:36
  • 1
    Your question assumes I'd have known it doesn't work with `public` - a thought that has never crossed my mind because why would one make data-members public anyway? This would violate data-capsuling! – pi84 Jun 30 '16 at 06:48
  • Basic problem solving skills. If you think it has anything to do with `protected`, test that hypothesis. It requires a tiny bit of thinking though, and I realise that isn't fashionable anymore. – juanchopanza Jun 30 '16 at 06:50
  • 1
    You don't need `typedef` in `typedef enum class mySubType{...}`, it wull be ignored and compiler also produces warning. – PcAF Jun 30 '16 at 06:56
  • @juanchopanza: my guess is you're jumping to conclusions. pi84 has clearly stated that it was made protected so it is accessible by derived classes, but still private to the outside. "Testing" it with public is irrational, as public and protected are both accessible by derived classes. So your problem solving skills would just make you waste time. Thanks for marking as a dup, though, I forgot to do that - sry, my bad. – St0fF Jun 30 '16 at 07:04
  • @St0fF No, I am not, and I am astounded that you think that a simple test to gain understanding and simplify a problem would be a waste of time. – juanchopanza Jun 30 '16 at 07:08
  • @juanchopanza I'm also astounded that you're jumping to conclusions, yet again. I just do not do tests that I _think_ have no potential to lead into the right direction, though sometimes my thoughts about that may also be wrong. Right now on my own problems I'm investing a lot of time to gain understanding and try to simplify. Just have a look at my last question (asked yesterday), maybe you can help solving it? – St0fF Jun 30 '16 at 07:24
  • @St0fF The only conclusion is that OP could work on their problem solving skills. I think there is enough evidence to reach that. – juanchopanza Jun 30 '16 at 08:10

1 Answers1

0

Sorry, that is not possible. See here: C++ error initializing base class data member in the derived class member initialization list

Basically you'll have to rewrite the base CTor declaration to accept that enum like this:

myBase( mySubType _t = mySubType::base ) : myType( _t ) {}

By making that CTor also accept no parameters, you already have defined a default CTor, which can come in handy when working with std containers.

And finally use it in your derived class that way:

myDerived::myDerived() : myBase( mySubType::type1 ) {}
Community
  • 1
  • 1
St0fF
  • 1,553
  • 12
  • 22