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. 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
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.
<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 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...
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.
<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
>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
<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: administrata |
last post by:
Is it possible?
I tried...
I = "John"
print \
"""
I used to love pizza"""
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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).
--...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |