Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is this allowed?

Reply
Thread Tools

Why is this allowed?

 
 
blangela
Guest
Posts: n/a
 
      10-02-2007
A student of my class submitted a solution where a derived class had
the exact same member data declared as the abstract base class that it
inherited from. Surprisingly, MS VS 2005 did not complain about this.

Assuming this is "standard c++", why would the language allow this?

I have not had a chance to experiment to find out if it is only
allowed when the data members are private (which is what I suspect).

Also, it it allowed because the base class is an abstract base class?

What is the benefit of this "feature"? In other words, why would a
programmer want to do this?

Cheers,

Bob

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      10-02-2007
On 2007-10-02 06:16:20 -1000, blangela <(E-Mail Removed)> said:

> A student of my class submitted a solution where a derived class had
> the exact same member data declared as the abstract base class that it
> inherited from. Surprisingly, MS VS 2005 did not complain about this.
>
> Assuming this is "standard c++", why would the language allow this?
>
> I have not had a chance to experiment to find out if it is only
> allowed when the data members are private (which is what I suspect).
>
> Also, it it allowed because the base class is an abstract base class?
>
> What is the benefit of this "feature"? In other words, why would a
> programmer want to do this?
>


Names are defined in scopes, and there's nothing that prohibits
defining the same name in multiple scopes. It's often useful.

// global scope:
void f();
int i;

// namespace scope:
namespace ns
{
void f();
int i;
}

namespace ns
{
namespace nested
{
void f();
int i;
}

// class scope:
struct S
{
void f();
int i;
};

struct D: S
{
void f();
int i;
};

In all cases, you can refer to the thing you actually want with
appropriate qualifiers:

f(); // global name
ns::f(); // namespace name
ns::nested::f(); // namespace name
&S::f; // class member name
&D::f; // class member name

Same thing for data members.

As to why, there's no good reason to prohibit it. In particular,
private data in a base class shouldn't interfere with names in derived
classes. And, more generally, if you don't care about the data members
in a base class, there's no reason to require your derived class to
avoid their names.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
 
 
 
Puppet_Sock
Guest
Posts: n/a
 
      10-02-2007
On Oct 2, 12:25 pm, Pete Becker <(E-Mail Removed)> wrote:
[snip]
> As to why, there's no good reason to prohibit it. In particular,
> private data in a base class shouldn't interfere with names in derived
> classes. And, more generally, if you don't care about the data members
> in a base class, there's no reason to require your derived class to
> avoid their names.


Indeed, if the data is private, there is good reason
not to require the derived class to avoid those names.
Or to put it more forcefully, there is good reason for
the derived class to have no awareness of the private
parts of the parent class. It is object oriented. That
way, the parent class can change those details, and the
derived class won't break.
Socks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments