473,491 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[noob]Multiple inheritance

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.

Jan 3 '06 #1
8 1448
On 3 Jan 2006 09:46:55 -0800, mm*****@gmail.com wrote:
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.

--
Bob Hairgrove
No**********@Home.com
Jan 3 '06 #2
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.

Jan 3 '06 #3

<mm*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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
Jan 3 '06 #4
mm*****@gmail.com wrote:
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-lit...heritance.html

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 3 '06 #5
Thanks for your reply!
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'.
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?
Do what?

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.

Jan 3 '06 #6

<mm*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks for your reply!
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'.


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.
Do what? 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
Jan 3 '06 #7
>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

Jan 3 '06 #8

<mm*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Define 'elegant'. As a newbie to programming I wouldn't really know.


So why did you ask about it?
Maybe you can help
me out.
First convince me that 'elegance' is needed.

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


Object Oriented Programming with C++ - E Balgurusamy


I'm not familiar with that one.

-Mike
Jan 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1735
by: administrata | last post by:
Is it possible? I tried... I = "John" print \ """ I used to love pizza"""
5
4840
by: utabintarbo | last post by:
I have a path spec similar to '/home/*/.mozilla/*/*/cache*/*' (this will probably look familiar to *nix users) with multiple wildcards. I am finding it difficult gathering ALL file pathnames which...
5
1731
by: Tony Vasquez | last post by:
Hi fellas. I have a problem. I am trying ot get a popup window to appear in the center of the screen, both vertically and horozontally. I am trying to display a large version of an image in this...
2
1376
by: Vincent ROGER | last post by:
I have a maybe a "noob" question to ask but I'd like to why that code does not compile and why more precisely it does not search for 'print()' method in 'Model'. Of course I could replace...
7
2444
by: Dave | last post by:
Hi, Maybe I'm missing something with the DataKeyField attribute of a datagrid but it seems that it's somewhat limiting since this only allows you to specify one field as the key. I have a...
10
1209
by: Shane | last post by:
Hello, I am new to VB.Net so go easy on me, now on to the question that I have. I would like to have multiple forms in my application, but I need help doing this. I know that I can inherit a...
8
1570
by: hiro | last post by:
Hi there, I'm very new to python, the problem I need to solve is whats the "best/ simplest/cleanest" way to read in multiple files (ascii), do stuff to them, and write them out(ascii). --...
30
1871
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
10
1173
by: Scott Townsend | last post by:
So I'm trying to Write a Backend to something and in testing I decided to create a generic front end app that can simulate the passed in Data. So I have 2 projects in my solution, though I'm...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.