Results 1 to 2 of 2

Thread: Inheritance Troubles

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    84

    Inheritance Troubles

    I have two classes, one of which is derived from the other.

    Code:
    class CPrimitive
    {
    public:
        CPrimitive();
        ~CPrimitive();
    
        void Function1() { Function2() };
        int Get_I() { return i; };
    
    protected:
        Function2() { i=1; };
        int i;
    }
    
    class CBox : public CPrimitive
    {
    public:
        CBox();
        ~CBox();
    
    protected:
        Function2() { i=2; };
    }
    
    //The inline functions are actualy declared in a .cpp file and are much
    //more complex, but this example still stands.
    
    //In my main code i do something like this
    CBox box = new CBox();
    box->Function1();
    
    int what_is_I =box->Get_I();
    I want Function1 to call the new definition of Function2 in the CBox class and have i=2 returned. However it calls the old Function2 from CPrimitive and returns i=1. Its there any way to get the code to reference the new Definition without redefining Function1 for CBox?

  2. #2
    Megatron
    Guest
    Change Function2 to a virtual function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width