-
February 2nd, 2009, 11:47 PM
#1
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.
-
February 3rd, 2009, 12:27 AM
#2
Re: Can a base class pointer call methods in the derived class?
-
February 3rd, 2009, 12:28 AM
#3
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.
-
February 3rd, 2009, 12:52 AM
#4
Re: Can a base class pointer call methods in the derived class?
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?
-
February 3rd, 2009, 12:56 AM
#5
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.
-
February 3rd, 2009, 01:01 AM
#6
Re: Can a base class pointer call methods in the derived class?
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:
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.
-
February 3rd, 2009, 01:14 AM
#7
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.
-
February 3rd, 2009, 01:21 AM
#8
Re: Can a base class pointer call methods in the derived class?
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.
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.
-
February 3rd, 2009, 01:27 AM
#9
Re: Can a base class pointer call methods in the derived class?
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).
-
February 3rd, 2009, 01:34 AM
#10
Re: Can a base class pointer call methods in the derived class?
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?
-
February 3rd, 2009, 02:59 AM
#11
Re: Can a base class pointer call methods in the derived class?
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).
-
February 3rd, 2009, 04:56 AM
#12
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.
-
February 3rd, 2009, 06:59 AM
#13
Re: Can a base class pointer call methods in the derived class?
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".
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
-
February 3rd, 2009, 08:20 AM
#14
Re: Can a base class pointer call methods in the derived class?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|