473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Member variable with the same name in base and derived?

In C++ is it possible to have a class B with a public member int i, and
to have a class D that derives from B, and define in that class D a
member also named i of type int?

Aug 24 '06 #1
10 2584
ma*********@hotmail.com wrote:
In C++ is it possible to have a class B with a public member int i,
and to have a class D that derives from B, and define in that class D
a member also named i of type int?
What do you think? Do you have a problem stemming from this situation?
Or is it just a homework assignment?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #2

Victor Bazarov schreef:
What do you think?
I think it is possible and comparable with have a variable with the
same name in an inner and outer loop in a program. It masks the
original name, it is absolutely stupid to do so, but syntactically it
would be allowed.
Do you have a problem stemming from this situation?
Or is it just a homework assignment?
It's a check question of some selfstudy material I am voluntary reading
at this moment. Actually it is about the same situation in Java. But as
a C++ programmer I wondered if this would also be in C++.

Unfortunately, I am well passed the age in which they would give me a
homework assignment. I used to look a lot better, and even more did the
women and girls of my age back then :-(

Aug 24 '06 #3

ma*********@hotmail.com schreef:
Actually it is about the same situation in Java.
And to add: since I am working in Java at my present project, I do not
have a C++ compiler at hand to test the outcome. If you know, you could
give a reply. Otherwise, and I can imagine you think I am a kid too
lazy to do his homework rereading my message, I will test some things
on my home PC this night, no problem.

Aug 24 '06 #4
ma*********@hotmail.com wrote:
ma*********@hotmail.com schreef:
> Actually it is about the same situation in Java.

And to add: since I am working in Java at my present project, I do not
have a C++ compiler at hand to test the outcome. If you know, you
could give a reply. Otherwise, and I can imagine you think I am a kid
too lazy to do his homework rereading my message, I will test some
things on my home PC this night, no problem.
http://www.comeaucomputing.com/tryitout/

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #5
ma*********@hotmail.com wrote:
Victor Bazarov schreef:
>What do you think?

I think it is possible and comparable with have a variable with the
same name in an inner and outer loop in a program. It masks the
original name, it is absolutely stupid to do so, but syntactically it
would be allowed.
It is not stupid to do so. And I am sure within five minutes I can
come up with a case or two where it would make all the sense to do just
that. I just don't have the five minutes.
>Do you have a problem stemming from this situation?
Or is it just a homework assignment?

It's a check question of some selfstudy material I am voluntary
reading at this moment. Actually it is about the same situation in
Java. But as a C++ programmer I wondered if this would also be in C++.
I don't know how it is in Java. Nor it is really topical to discuss it
here. But in C++ it is allowed.
Unfortunately, I am well passed the age in which they would give me a
homework assignment. I used to look a lot better, and even more did
the women and girls of my age back then :-(
What age is that? I am wondering because I seem to have homework all
the time (although it may not be called that), and I do it, just like
most of my colleagues. Doing one's homework is a necessity; the only
thing defeats the purpose of it is if somebody else is doing it for
you.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #6

Victor Bazarov schreef:
It is not stupid to do so.
Two private variable members with the same name perhaps, but public
data members? Keep in mind that it is the public that would make the
variable possibly accessible in the inherited class, and public data
members in a class are a bad thing almost always.

Nevertheless, yes, I always do my homework, I did have some idea what
the answer was, I just wanted an expert like you to give me some
comfirmation that I was thinking right. We must always all do some
homework to keep on track, you're right.

Aug 24 '06 #7
ma*********@hotmail.com wrote:
Victor Bazarov schreef:
>It is not stupid to do so.

Two private variable members with the same name perhaps, but public
data members?
You said nothing about the access specifier for the member in class D,
although it is possible to conclude that you were talking of public
in both classes.
Keep in mind that it is the public that would make the
variable possibly accessible in the inherited class, and public data
members in a class are a bad thing almost always.
Again, not true. Were they a bad thing, they would probably be more
difficult to declare/manage in C++. They aren't. Which means, public
members are just fine, AFA the language is concerned.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #8

Victor Bazarov schreef:
Again, not true. Were they a bad thing, they would probably be more
difficult to declare/manage in C++.
Well not everything you can declare in a language is good design, I
just read somewhere that it's better never to use public members
because:

1. Then you have a consistent interface only existing of functions
2. If you lateron add something to the getter it is all directly in the
code using the class.

That would not apply to really all the applications, but would be a
good rule of thumb, I hope?

Aug 24 '06 #9
ma*********@hotmail.com wrote:
Victor Bazarov schreef:
>Again, not true. Were they a bad thing, they would probably be more
difficult to declare/manage in C++.

Well not everything you can declare in a language is good design, I
just read somewhere that it's better never to use public members
because:

1. Then you have a consistent interface only existing of functions
2. If you lateron add something to the getter it is all directly in
the code using the class.

That would not apply to really all the applications, but would be a
good rule of thumb, I hope?
Not to step on any aching corns or something like that, but are you
limiting the view of C++ to only one paradigm, the OOP part of it?

Public data members are fun and useful and have their place in all of
our activities as long as we don't forget what we're doing and for what.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #10

Victor Bazarov schreef:
Not to step on any aching corns or something like that, but are you
limiting the view of C++ to only one paradigm, the OOP part of it?
Well, I guess yes I am.
Public data members are fun and useful and have their place in all of
our activities as long as we don't forget what we're doing and for what.
I will keep it in mind, thanks for all your advice.

Aug 25 '06 #11

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: none | last post by:
Hi, I have a base class with a pointer-to-member function variable. Then I have a derived class that needs to use that variable to call a member function (with the same arguments and return value...
2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
5
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
4
by: RSH | last post by:
Hi, I have a situation where I have a class which manages different data connections. In trying to enforce encapsulation I have a member field called connection which could be a type of...
3
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.