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?
Re: Can a base class pointer call methods in the derived class?
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.
Re: Can a base class pointer call methods in the derived class?
Quote:
Originally Posted by
laserlight
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?
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.
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).
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
Re: Can a base class pointer call methods in the derived class?
Quote:
Originally Posted by
laserlight
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. :rolleyes:
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. :)
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).
Re: Can a base class pointer call methods in the derived class?
Quote:
Originally Posted by
laserlight
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?
Re: Can a base class pointer call methods in the derived class?
Quote:
Originally Posted by
ZhiYi
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).
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.
Re: Can a base class pointer call methods in the derived class?
Quote:
Originally Posted by
Peter_APIIT
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".
Re: Can a base class pointer call methods in the derived class?
Quote:
Originally Posted by
ZhiYi
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.