Why is this allowed?

Discussion in 'C++' started by blangela, Oct 2, 2007.

  1. blangela

    blangela Guest

    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
     
    blangela, Oct 2, 2007
    #1
    1. Advertising

  2. blangela

    Pete Becker Guest

    On 2007-10-02 06:16:20 -1000, blangela <> 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)
     
    Pete Becker, Oct 2, 2007
    #2
    1. Advertising

  3. blangela

    Puppet_Sock Guest

    On Oct 2, 12:25 pm, Pete Becker <> 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
     
    Puppet_Sock, Oct 2, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RWR3YXJkIFNtaXQ=?=
    Replies:
    1
    Views:
    323
    =?Utf-8?B?RWR3YXJkIFNtaXQ=?=
    May 21, 2004
  2. =?Utf-8?B?aW5ldG11Zw==?=

    Why is there no "runtime" allowed in the ASP.NET web.config

    =?Utf-8?B?aW5ldG11Zw==?=, Feb 11, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    3,315
    Scott Allen
    Feb 14, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    935
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,130
    Smokey Grindel
    Dec 2, 2006
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    535
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page