Troubles overriding a const in a derived class

This is a discussion on Troubles overriding a const in a derived class within the C++ Programming forums, part of the General Programming Boards category; I have a Base class and a Derived class, only the parts of the class in question are listed for ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    17

    Troubles overriding a const in a derived class

    I have a Base class and a Derived class, only the parts of the class in question are listed for brevity:
    Code:
    class Base
    	{
    public:
    	static const int type;
    
    	Base (int x=0, int y=0);
    	~Base();
                   };
    
    class Derived : public Base
                   {
    public:
    	static const int type; 
    
    	Derived (int x=0, int y=0);
    	~Derived();
                   };
    
    const int Base::type = 0;
    const int Derived::type = 1;
    
    Base::Base (int x, int y)
    		{
                    cerr << "You created an object of type " << type;
    		}
    Base::~Base()
    		{
                    cerr << "Destructor called for object of type " << type;
    		}
    
    Derived::Derived (int x, int y) : Base (x, y)
    		{}  
    
    Derived::~Derived()
    		{}
    Now in my code I declare an array of pointers which will be used to point to objects of either the Base or Derived Classes:

    Code:
    Base *ClassPointers[2];
    
    ClassPointers[0] = new Base();
    ClassPointers[1] = new Derived();
    Now if I look in my error log, I see these messages:
    You created an object of type 0
    You created an object of type 0
    Of course, I expect the messages to look like this:
    You created an object of type 0
    You created on object of type 1
    What gives? Help would be greatly appreciated.
    -sh0x

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    I think it's because the derived class calls the base constuctor which you use Base::type in. It will always be Base::type whenever that constructor is called not Derived::type like you want it to be.

    Code:
    Base::Base (int x, int y)
    {
         cerr << "You created an object of type " << type;  // type refers to Base::type
    }
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    17
    So how can I solve this problem? The Base class' constructor is always called after the Derived class' constructor when a Derived class is instantiated, right? And just because the constructor for Base reports 0 for type, does that mean Derived::type is actually equal to 0 or is it equal to 1? I need this variable for my program to identify what derivative of the class it is (I'm actually going to rewrite Base as a pure virtual class and only instantiate derived classes) Or can you think of a better way for me to achieve this?
    -sh0x

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    Just use a virtual method.

    e.g.
    class Base {
    ....
    virtual int GetInstanceType();
    ....

    override it in every descendant of the Base class and
    avoid invoking any virtual methods in the constructors and all be ok.

    You can make this method abstract(pure virtual by adding =0 in the method declaration) in order to force the users of the Base class to implement it.

    damyan

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    17
    Originally posted by damyan
    Just use a virtual method.

    e.g.
    class Base {
    ....
    virtual int GetInstanceType();
    ....

    override it in every descendant of the Base class and
    avoid invoking any virtual methods in the constructors and all be ok.
    I did try this and I don't like the way it works. It does work if I don't call the GetInstanceType() parameter in the constructor, but the problem is that I do need to know what type the instance is inside the base constructor. For instance, for error logging purposes I have the following line in the base constructor (and yes I had to un-pure-virtualize the GetInstanceType function to make this work):

    Write_Error("\nCreated instance of type %i", bob[GetInstanceType()]");

    And it always states in the error log that an instance of type 0 was created. To have to delete this line from the constructor and re-write it in every derived class would be a real pain. I mean, isn't this what deriving classes is for? Convenience and reduction of code duplication? Is there any way to solve this problem? If not, can you tell me exactly why this happens so I can at least understand why it does not work?
    -sh0x

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    It happens because you are declaring TWO variables named type, one in the parent class, one in the derived. It IS legal C++ to do this, although it's not a good idea.

    In this case, when you are inside the parent class's constructor, the parent only "sees" the variables that are part of its class -- so it sees only the parent's copy of type.

    I don't believe you CAN know, when you are in a parent's constructor, if you are in the base or derived class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21