C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overriding of member variables in inherited classes?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
konsta
Guest





PostPosted: Mon Nov 22, 2004 11:14 am    Post subject: Overriding of member variables in inherited classes? Reply with quote



Hi all,
I'd like to have a base class P (say a person) that has a pointer to
another class C (say a car) and then a class M (say manager) which is
a derived class of P that has a pointer to a class R (say Rolls Royce)
which is a derived class of C. The assumption is that all M's will
have pointers to C's which are at least R's.

What's the best way of accomplishing this? Can I e.g. in the
definition of M just define a member variable R* which has the same
name as the C* in the definition of P?

At least Visual C++ seems to think the code below is OK (and
furthermore even automatic statement completion works) but I want to
make sure it is portable. Which version of p_ will be given by a class
(i.e. if m is a variable of type M will m.p_ refer to M::p_ and not
P::p_)? Thanks!

#include <iostream>
#include <string>

using namespace std;

struct car{
car(){};
virtual void brand(){ cout << "carn";}
};

struct rollsroyce: public car{
rollsroyce( long code = 17): her_majestys_secret_code_( code){};
virtual void brand(){ cout << "Rolls Roycen";}
long her_majestys_secret_code_;
};

struct person{
person( car* p): p_( p){};
car* p_;
virtual void make_of_car(){ p_->brand();}
};

struct manager: public person{
manager( rollsroyce* p): person( p){
p_ = p;};
rollsroyce* p_;
};

int main(int argc, char* argv[])
{
car c;
rollsroyce r;

manager m = manager( &r);
m.make_of_car(); // prints out "Rolls Royce"
m.p_->her_majestys_secret_code_ = 17; // ok

return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Mang
Guest





PostPosted: Mon Nov 22, 2004 10:00 pm    Post subject: Re: Overriding of member variables in inherited classes? Reply with quote




"konsta" <uniquecomb (AT) yahoo (DOT) com> schrieb im Newsbeitrag
news:d0290665.0411212115.238459ff (AT) posting (DOT) google.com...
Quote:
Hi all,
I'd like to have a base class P (say a person) that has a pointer to
another class C (say a car) and then a class M (say manager) which is
a derived class of P that has a pointer to a class R (say Rolls Royce)
which is a derived class of C. The assumption is that all M's will
have pointers to C's which are at least R's.

Your solution contains two pointers in M.

Why not making P an (abstract) base class without data members, and then
deriving two-fold from it: 1) manager, 2) concrete_person, each holding a
pointer to one of your vehicles ?



Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.