CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    Can a base class pointer call methods in the derived class?

    I have a base class pointer which is assigned a derived class. Can this pointer call methods in the derived class which are not declared in the base class?

    i.e.

    class BaseClass
    {
    ........
    };

    class DerivedClass : public BaseClass
    {
    void Print(); //declared in DerivedClass only
    .......
    };


    BaseClass* ReturnClassType()
    {
    //some method logic here
    .............

    DerivedClass* dc;
    return dc;
    }


    BaseClass* bc = ReturnClassType();
    bc->Print();

    When I called print() using the base class pointer, there is a compile error saying that the method is not found in the base class.

    Is it possible for base class pointers to call methods declared in the derived class only?
    Last edited by ZhiYi; February 3rd, 2009 at 12:49 AM.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Can a base class pointer call methods in the derived class?

    No.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

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

    Re: Can a base class pointer call methods in the derived class?

    You would need to static_cast or dynamic_cast the base class pointer to an appropriate derived class pointer.
    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

  4. #4
    Join Date
    Apr 2005
    Posts
    200

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by laserlight View Post
    You would need to static_cast or dynamic_cast the base class pointer to an appropriate derived class pointer.
    I have tried using this:

    BaseClass* bc = dynamic_cast<DerivedClass*>ReturnClassType();
    bc->Print();


    But there was still an error. Is the dynamic cast done correctly?

  5. #5
    Join Date
    Aug 2007
    Posts
    135

    Re: Can a base class pointer call methods in the derived class?

    It would be a terrible design, if your base case had any information related to any of its derived classes. A base class should never ever ever refer to any of its derived classes.
    A base class represents something generic, while a derived class specializes that generic to something more specific.

    For example, if you have a base class Car, and a derived class Bugatti Veyron or a McLaren F1, the base class cannot assume that all cars are fast and have a method called DriveFasterThan200Mph, obviously all cars don't. Even worse, have a method called DriveFasterThan222Mph in the derived class, and the base class calling it.....makes no sense at all. No wonder your compiler complains when you refer to the derived class from the base class.
    Last edited by ChessMaster; February 3rd, 2009 at 01:03 AM.

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

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by ZhiYi
    But there was still an error. Is the dynamic cast done correctly?
    Next time state the error message. You probably wanted to write:
    Code:
    DerivedClass* ptr = dynamic_cast<DerivedClass*>(ReturnClassType());
    if (ptr)
    {
        ptr->Print();
    }
    But that is assuming that you implemented ReturnClassType() to return a valid or null BaseClass pointer. At the moment it looks like you are returning an uninitialised pointer. What exactly is ReturnClassType() supposed to do in the first place? After all, the main point of accessing a derived class object through a base class pointer is for polymorphism, but if you are not actually using polymorphism, it could be a case where the base class pointer is unnecessary to begin with.

    Oh, and remember to post your well indented code in [code][/code] bbcode tags.

    EDIT:
    Quote Originally Posted by ChessMaster
    It would be a terrible design, if your base case had any information related to any of its derived classes. A base class should never ever ever refer to any of its derived classes.
    A base class represents something generic, while a derived class specializes that generic to something more specific.
    Yes, but ZhiYi is talking about calling derived class specific member functions via the base class pointer, not about base class member functions calling derived class specific member functions (besides overrides of virtual functions, which are not really specific to the derived class in the first place).
    Last edited by laserlight; February 3rd, 2009 at 01:05 AM.
    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

  7. #7
    Join Date
    Aug 2007
    Posts
    135

    Re: Can a base class pointer call methods in the derived class?

    If you want, a method in the derived class to execute when you make a call from the base class, then read about polymorphism.
    In the car example, if you want to start your McLaren then you can have a StartMyAwesomeCar method in the base class Car (All cars are awesome, and they better start), and then derive the McLaren class from Car class & then override the method StartMyAwesomeCar in the derived class McLaren.
    Now if you create a derived class object - an object of McLaren, and call StartMyAwesomeCar from the base class using the base class pointer, it will call the derived class method.

    I wud suggest to read about polymorphism for the same
    Last edited by ChessMaster; February 3rd, 2009 at 01:28 AM.

  8. #8
    Join Date
    Aug 2007
    Posts
    135

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by laserlight View Post
    Yes, but ZhiYi is talking about calling derived class specific member functions via the base class pointer, not about base class member functions calling derived class specific member functions (besides overrides of virtual functions, which are not really specific to the derived class in the first place).
    I presume you missed the following in the OP's post.

    Quote Originally Posted by ZhiYi
    When I called print() using the base class pointer, there is a compile error saying that the method is not found in the base class.

    Is it possible for base class pointers to call methods declared in the derived class only?
    This says he is trying to call the derived class method using the base class pointer, while the base class has not defined it. Obviously, he is new to object oriented programming.

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

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by ChessMaster
    This says he is trying to call the derived class method using the base class pointer, while the base class has not defined it.
    Yes, but the point is that the derived class member function is supposed to be specific to the derived class (hence the question whether it is "possible for base class pointers to call methods declared in the derived class only"). From what I understand, ZhiYi does not want to provide the base class with a virtual Print() member function. This could be a design issue (e.g., polymorphism is actually desired), or it could be a usage issue (e.g., the base class pointer is unnecessary to begin with).
    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

  10. #10
    Join Date
    Aug 2007
    Posts
    135

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by laserlight View Post
    Yes, but the point is that the derived class member function is supposed to be specific to the derived class (hence the question whether it is "possible for base class pointers to call methods declared in the derived class only"). From what I understand, ZhiYi does not want to provide the base class with a virtual Print() member function. This could be a design issue (e.g., polymorphism is actually desired), or it could be a usage issue (e.g., the base class pointer is unnecessary to begin with).
    The OP needs to clarify what exactly he is trying to achieve. From the naming of classes, and him trying to call methods in derived class from base class, i presume it is lack of understanding of OOPS.

    ZhiYi, what exactly are you trying to achieve?

  11. #11
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by ZhiYi View Post
    Is it possible for base class pointers to call methods declared in the derived class only?
    No, you can only call methods that are part of the class. Kind of obvious isn't it?. You cannot use stuff that isn't there.

    In this case you only need to declare a virtual print method in the base class and it will work. Because print is now declared in the base class it can be called via a base class pointer. Furthermore it's declared virtual which means it's overridable by derived classes. And finally it's actually overridden in a derived class so it's the derived class version of print that's gonna be called (if the base class pointer in fact is a pointer to the derived object).

  12. #12
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Can a base class pointer call methods in the derived class?

    This is a bad design where you cast a base class pointer(General) to derived class pointer(Specific) and vice versa should be ok which is slicing problem.
    Thanks for your help.

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by Peter_APIIT View Post
    This is a bad design where you cast a base class pointer(General) to derived class pointer(Specific) and vice versa should be ok which is slicing problem.
    Casting a derived pointer to a bae pointer has NOTHING to do with slicing.

    An object gets sliced when a base class instance is copy constructed from a derived class. The new object is only of the base class type and all of the derived functionallity has been "sliced off".
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Can a base class pointer call methods in the derived class?

    Quote Originally Posted by ZhiYi View Post
    I have a base class pointer which is assigned a derived class. Can this pointer call methods in the derived class which are not declared in the base class?

    i.e.

    class BaseClass
    {
    ........
    };

    class DerivedClass : public BaseClass
    {
    void Print(); //declared in DerivedClass only
    .......
    };


    BaseClass* ReturnClassType()
    {
    //some method logic here
    .............

    DerivedClass* dc;
    return dc;
    }


    BaseClass* bc = ReturnClassType();
    bc->Print();

    When I called print() using the base class pointer, there is a compile error saying that the method is not found in the base class.

    Is it possible for base class pointers to call methods declared in the derived class only?
    No. That's what virtual functions are for. You'd want to create a virtual Print function in the base class and override it where appropriate in the derived classes.

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