CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    440

    Base Class's function gets called

    Here are the classes:

    BaseClass.h
    Code:
    class BaseClass
    {
        public:
            BaseClass();
            virtual ~BaseClass();
            virtual void printStuff() const;
        protected:
        private:
    };
    BaseClass.cpp
    Code:
    BaseClass::BaseClass()
    {
        //ctor
    }
    
    BaseClass::~BaseClass()
    {
        //dtor
    }
    
    void BaseClass::printStuff() const
    {
        std::cout << "BaseClass" << std::endl;
    }
    DerivedClass.h
    Code:
    class DerivedClass : public BaseClass
    {
        public:
            DerivedClass();
            virtual ~DerivedClass();
            virtual void printStuff() const;
        protected:
        private:
    };
    DerivedClass.cpp
    Code:
    DerivedClass::DerivedClass()
    {
        //ctor
    }
    
    DerivedClass::~DerivedClass()
    {
        //dtor
    }
    
    void DerivedClass::printStuff() const
    {
        std::cout << "DerivedClass" << std::endl;
    }
    And here's main():
    Code:
    int main()
    {
        DerivedClass* aDC = new DerivedClass();
        vector<BaseClass*> aVec;
        aVec.push_back( aDC );
        aVec[0]->printStuff();
        delete aDC;
        return 0;
    }
    When I call printStuff, the DerivedClass's function gets called. Now, if I remove the const part from the DerivedClass's printStuff function, we call the BaseClass's printStuff function. Can anyone explain why this happens? I tried a Google search, but not quite sure how to word this.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Base Class's function gets called

    If you do not declare the member function as const, then you are overloading, not overriding, the member function to have a non-const version. Thus, the polymorphism that comes with an override does not apply. I expect that the base class' version of the member function becomes hidden in the derived class (i.e., you would get an error if you tried calling it with a const DerivedClass object).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Base Class's function gets called

    Was the code edited since laserlight's post? Because it looks like it's declared const to me.

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: Base Class's function gets called

    No, it wasn't. I just said "...Now, if I remove the const part from the DerivedClass's printStuff function...". If I leave the code the way it the way is, things behave as I would expect them to. But, if I remove the const from the DerivedClass's function, then the BaseClass's printStuff function is called. I think laserlight's explanation makes sense.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Base Class's function gets called

    Note that C++11 has introduced the override keyword to flag this as a compiler error.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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

Featured