[noob]Multiple inheritance

M

mmu2643

Hi,

I had a question regarding multiple inheritance.

class B1{
public:
int a;

};

class B2{
public:
int a;

};

class D : public B1, public B2{

};

Now in class D how do we access the member item 'a' without resulting
in a name collision? Can someone please explain this to me? TIA.
 
B

Bob Hairgrove

Hi,

I had a question regarding multiple inheritance.

class B1{
public:
int a;

};

class B2{
public:
int a;

};

class D : public B1, public B2{

};

Now in class D how do we access the member item 'a' without resulting
in a name collision? Can someone please explain this to me? TIA.

You can explicitly qualify the names (e.g. "B1::a" or "B2::a" within
the class scope of D; "D::B1::a" or "D::B2::a" in an enclosing scope).

As an aside note, it is not a good idea to have public data members.
 
M

mmu2643

Thanks for your response. I had another question(just clarifying
actually):

class B{
public:
int a;
};

class D : public B{
public:
int a;
void f1(int a){};
};

Now in a member function in D, 'a' always refers to the derived class
verion of 'a' (i.e. D::a) and not B::a, is that right?

And to access the Base class version of 'a' in a function in D, I use
B::a? I just wanted to clarify this because I always thought that
access to a member of a class must be done in conjunction with an
object or pointer to an object of that class as opposed to the class
name as in B1::a or B2::a.

Also in D::f1 can I access D::a as this->a (and B::a as B::a) or is
there a better way to do it? TIA.
 
M

Mike Wahler

Thanks for your response. I had another question(just clarifying
actually):

class B{
public:
int a;
};

class D : public B{
public:
int a;
void f1(int a){};
};

Now in a member function in D, 'a' always refers to the derived class
verion of 'a' (i.e. D::a) and not B::a, is that right?

No, in your function 'f1()', 'a' refers to its parameter,
not the data member. This is an example of 'name hiding'.
And to access the Base class version of 'a' in a function in D, I use
B::a?
Yes.

I just wanted to clarify this because I always thought that
access to a member of a class must be done in conjunction with an
object or pointer to an object of that class as opposed to the class
name as in B1::a or B2::a.

It depends upon the member. Access to a nonstatic member requires
an object, yes. For static members, no.

Also in D::f1 can I access D::a as this->a (and B::a as B::a)

Yes. And the way you have it (with your parameter with the same
name), this explicitness is required.
or is
there a better way to do it? TIA.

Do what? Perhaps if you give us some idea of what problem
you're actually trying to solve, we could give more specific
advice.

-Mike
 
B

Ben Pope

Thanks for your response. I had another question(just clarifying
actually):

class B{
public:
int a;
};

class D : public B{
public:
int a;
void f1(int a){};
};

Now in a member function in D, 'a' always refers to the derived class
verion of 'a' (i.e. D::a) and not B::a, is that right?

And to access the Base class version of 'a' in a function in D, I use
B::a? I just wanted to clarify this because I always thought that
access to a member of a class must be done in conjunction with an
object or pointer to an object of that class as opposed to the class
name as in B1::a or B2::a.

Also in D::f1 can I access D::a as this->a (and B::a as B::a) or is
there a better way to do it? TIA.

When using multiple inheritance, it's almost always a good idea to only
inherit publicly from pure abstract classes. This way, the inheritance
is for implementing an interface to provide flexibility, and not for
code re-use.

In most cases, this fixes most of the issues that result from [improper
use of] multiple inheritance.

Using public inheritance as a method of code re-use is almost always A
Bad Thing.

Take a look at the FAQ on multiple inheritance if you have not already
done so:

http://www.parashift.com/c++-faq-lite/multiple-inheritance.html

Ben Pope
 
M

mmu2643

Thanks for your reply!
No, in your function 'f1()', 'a' refers to its parameter,
not the data member. This is an example of 'name hiding'.

Assuming I didn't have 'void f1(int a){};' then would 'a' always refer
to the derived class version of 'a' and not B::a?
Is there a more elegant way to access D::a(and B::a) in f1() as opposed
to the one I mentioned?

Actually I am new to the language so I am just trying to grasp the
concepts - I am not trying to solve any problem in particular.
 
M

Mike Wahler

Thanks for your reply!



Assuming I didn't have 'void f1(int a){};' then would 'a' always refer
to the derived class version of 'a' and not B::a?

If there's no other 'a' in scope to hide it.
Is there a more elegant way to access D::a(and B::a) in f1() as opposed
to the one I mentioned?

Define 'elegant'. Better yet, stop worrying about such
vague concepts, and be concerned with correctness.

Actually I am new to the language so I am just trying to grasp the
concepts - I am not trying to solve any problem in particular.

C++ has no concept of 'elegance'. It does however have the
(important) concept of 'scope', with which you seem to be
having trouble.

Which C++ book(s) are you reading?

-Mike
 
M

mmu2643

Define 'elegant'.
As a newbie to programming I wouldn't really know. Maybe you can help
me out.
Which C++ book(s) are you reading?

Object Oriented Programming with C++ - E Balgurusamy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top