C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

usage of PRIVATE members in derived class without errors?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
mario semo
Guest





PostPosted: Fri Dec 08, 2006 6:52 am    Post subject: usage of PRIVATE members in derived class without errors? Reply with quote



Hello,

in the following sample the "typedef ClassName Inherited" is privat in all
classes except the top level class, where it is not defined, BUT USED!
so the MainClass uses "Inherited::" which is a private typedef from
baseclasses.

The code compiles fine with
IBM VACPP 3.5 (WinNT compiler)
MSVC++ 6.x
GCC 2.95.2-6
(have not tried other compiles).

Why can the derived class access the privat typedef from the baseclasses?


#include <stdio.h>

class Base
{

//--------------------------------------------------------------------------
private:

//--------------------------------------------------------------------------

typedef Base Inherited;


//--------------------------------------------------------------------------
public:

//--------------------------------------------------------------------------

Base() {}
virtual ~Base() {}
virtual void foo() {printf("base::foo\n");}

};

class Derived
: public Base
{

//--------------------------------------------------------------------------
private:

//--------------------------------------------------------------------------

typedef Base Inherited;


//--------------------------------------------------------------------------
public:

//--------------------------------------------------------------------------

Derived(){}
virtual ~Derived(){}
virtual void foo() {printf("Derived::foo\n");}
};

class MainC
: public Derived
{

//--------------------------------------------------------------------------
public:

//--------------------------------------------------------------------------
MainC() {}
virtual ~MainC(){}

virtual void foo()
{
printf("---1---\n");
Inherited::foo();
printf("---2---\n");
}

};

int main (int argc,char *argv[])
{
MainC aM;
aM.foo();

return 0;
}



--
mit freundlichen Grüßen/best regards
mario semo




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel Krügler
Guest





PostPosted: Sat Dec 09, 2006 2:59 am    Post subject: Re: usage of PRIVATE members in derived class without errors Reply with quote



mario semo schrieb:

Quote:
in the following sample the "typedef ClassName Inherited" is privat in all
classes except the top level class, where it is not defined, BUT USED!
so the MainClass uses "Inherited::" which is a private typedef from
baseclasses.

The code compiles fine with
IBM VACPP 3.5 (WinNT compiler)
MSVC++ 6.x
GCC 2.95.2-6
(have not tried other compiles).

Why can the derived class access the privat typedef from the baseclasses?

Obviously these compilers all behave non-compliant in this usecase.
The typedef should not be accessible in your described scenario.
E.g. Comeau online (http://www.comeaucomputing.com/tryitout/) does
not accept your code.

Greetings from Bremen,

Daniel Krügler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Kanze
Guest





PostPosted: Sat Dec 09, 2006 3:00 am    Post subject: Re: usage of PRIVATE members in derived class without errors Reply with quote



mario semo wrote:

Quote:
in the following sample the "typedef ClassName Inherited" is privat in all
classes except the top level class, where it is not defined, BUT USED!
so the MainClass uses "Inherited::" which is a private typedef from
baseclasses.

The code compiles fine with
IBM VACPP 3.5 (WinNT compiler)
MSVC++ 6.x
GCC 2.95.2-6
(have not tried other compiles).

I don't know about the IBM compiler, but the other two are very
old. G++, for example, was very careless in its enforcing of
private for a long time. Your code is illegal, and doesn't
compile with more recent versions of g++. Nor with Sun CC 5.8,
the other recent compiler I have immediate access to.

--
James Kanze (GABI Software) email:james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.