By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,415 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

simple inheritance(?) question

P: n/a
Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;
} else {
base = new Class2;
}

I know it works, but is it somehow dodgy? I have a feeling there is a better
way but I'm not sure what to search for.

Thanks for your help.

Regards

Michael
Mar 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 9 Mrz., 11:22, "Michael" <michael5...@yahoo.comwrote:
Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;} else {

base = new Class2;

}

I know it works, but is it somehow dodgy? I have a feeling there is a better
way but I'm not sure what to search for.

Thanks for your help.

Regards

Michael

What you have discovered is the base class pointer. What you are doing
is a common practiced method for polymorphism. Just have a look at
http://www.cplusplus.com/doc/tutorial/polymorphism.html for instance.
Keep in Mind that with a base class pointer you can only access
members that are defined in the base class.

Regards

Stephan

Mar 9 '07 #2

P: n/a
On Mar 9, 5:22 am, "Michael" <michael5...@yahoo.comwrote:
Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;} else {

base = new Class2;

}

I know it works, but is it somehow dodgy? I have a feeling there is a better
way but I'm not sure what to search for.
As another poster has said already, this is standard
stuff in C++. You just have to be sure that the classes
involved have the right stuff as regards virtual functions,
public/protected/private members, etc.

You want to read the FAQ here.
http://www.parashift.com/c++-faq-lite/
Scroll down to the parts about inheritance.
Socks

Mar 9 '07 #3

P: n/a
Dnia Fri, 09 Mar 2007 04:19:28 -0800, dasteph napisał(a):
What you have discovered is the base class pointer. What you are
doing is a common practiced method for polymorphism. Just have a
look at http://www.cplusplus.com/doc/tutorial/polymorphism.html
for instance. Keep in Mind that with a base class pointer you can
only access members that are defined in the base class.
But keep in mind also that if that members are virtual
member functions, the overriden functions from the derived
classes will be called ;)

--
SasQ
Mar 9 '07 #4

P: n/a

<da*****@gmail.comwrote in message
news:11********************@v33g2000cwv.googlegrou ps.com...
On 9 Mrz., 11:22, "Michael" <michael5...@yahoo.comwrote:
>Hi All,

if I have a class BaseClass and two derived classes Class1 and Class2 and
I
don't know which derived class I will need, is it ok for me to do the
following:

BaseClass* base;

if(some_test){
base = new Class1;} else {

base = new Class2;

}

I know it works, but is it somehow dodgy? I have a feeling there is a
better
way but I'm not sure what to search for.

Thanks for your help.

Regards

Michael


What you have discovered is the base class pointer. What you are doing
is a common practiced method for polymorphism. Just have a look at
http://www.cplusplus.com/doc/tutorial/polymorphism.html for instance.
Keep in Mind that with a base class pointer you can only access
members that are defined in the base class.

Regards

Stephan
Thanks for your help.

I am a bit confused by my ability to use a member attribute that is only
present in Class1, not in BaseClass, after I do the base = new Class1.

I would have expected that I could only access functions and variables
defined in the BaseClass, and that any additional functionality (including
additional attributes) added by Class1 or Class2 would be lost, but this
appears to not be the case. It seems that since I am using the redefined
function from Class1 it also has access to the member attributes from
Class1. Would this be correct?

Can anyone explain why for me?

Thanks

Michael
Mar 10 '07 #5

P: n/a
Michael wrote:
>
I am a bit confused by my ability to use a member attribute that is only
present in Class1, not in BaseClass, after I do the base = new Class1.

I would have expected that I could only access functions and variables
defined in the BaseClass, and that any additional functionality (including
additional attributes) added by Class1 or Class2 would be lost, but this
appears to not be the case. It seems that since I am using the redefined
function from Class1 it also has access to the member attributes from
Class1. Would this be correct?
Yes it is, but only if the that redefined function is virtual.
Can anyone explain why for me?
Because it is virtual, when that function is called, it goes through the
vtable (virtual table of function pointers) to call the function. It
there by 'knows' what object it really is.
Adrian
Mar 10 '07 #6

P: n/a

"Adrian Hawryluk" <ad**************************@nospam.comwrote in message
news:8PnIh.52128$cE3.17324@edtnps89...
Michael wrote:
>>
I am a bit confused by my ability to use a member attribute that is only
present in Class1, not in BaseClass, after I do the base = new Class1.

I would have expected that I could only access functions and variables
defined in the BaseClass, and that any additional functionality
(including additional attributes) added by Class1 or Class2 would be
lost, but this appears to not be the case. It seems that since I am using
the redefined function from Class1 it also has access to the member
attributes from Class1. Would this be correct?

Yes it is, but only if the that redefined function is virtual.
>Can anyone explain why for me?

Because it is virtual, when that function is called, it goes through the
vtable (virtual table of function pointers) to call the function. It
there by 'knows' what object it really is.
Adrian
ahhhhh......ok....thanks
Mar 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.