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 

Re: typedef private or public ?

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





PostPosted: Fri Jan 23, 2004 2:27 pm    Post subject: Re: typedef private or public ? Reply with quote



The following piece of code works just fine .
even though typedef int T has been declared private , easily used in
main.
(I am assuming it's because the name T is visible in main too - correct
?)
Does it make a difference (in terms of good design ) if I make it
private or public ?

#include <iostream>
class A {
private :
typedef int T;
} ;
int main(){
A::T i ;
i = 17 ;
cout << i << endl ;
}

regards,
Aman.



--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

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





PostPosted: Sat Jan 24, 2004 1:06 pm    Post subject: Re: typedef private or public ? Reply with quote



Aman wrote:

Quote:
The following piece of code works just fine .
even though typedef int T has been declared private , easily used in
main.

This seems like a problem with your compiler.

Quote:
(I am assuming it's because the name T is visible in main too -
correct ?)

No. It is private to A, so cannot be used in main.

Quote:
Does it make a difference (in terms of good design ) if I make it
private or public ?

Just like with anything else you can define in your class, if it is part
of the public interface, make it public. If it's only internal, make it
private.

Quote:
#include class A {
private :
typedef int T;
} ;
int main(){
A::T i ;
i = 17 ;
cout << i << endl ;
}

When I try to compile the above code, my compiler says:

aman.cpp: In function `int main()':
aman.cpp:4: error: `typedef int A::T' is private
aman.cpp:7: error: within this context
aman.cpp:9: error: `cout' undeclared (first use this function)
aman.cpp:9: error: (Each undeclared identifier is reported only once for
each
function it appears in.)
aman.cpp:9: error: `endl' undeclared (first use this function)


This output was produced with g++ 3.3.2. The fact that your compiler
seems to know cout and endl, even though you don't properly qualify
them indicates that you might be using a rather old compiler, and maybe
that compiler doesn't respect the privacy of your A as it should.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Sat Jan 24, 2004 1:07 pm    Post subject: Re: typedef private or public ? Reply with quote



Aman wrote:
Quote:

The following piece of code works just fine.
even though typedef int T has been declared private,
easily used in main.
(I am assuming it's because the name T is visible
in main too - correct?)
Compiler bug? What compiler (brand/version) are you using?


Quote:
#include <iostream
class A {
private :
typedef int T;
} ;
int main(){
A::T i ;
i = 17 ;
cout << i << endl ;
}
This compiles neither with gcc 3.3.1 nor with

only because of the missing "std::" for "cout"
and "endl" but also because of the inaccessible
typedef.

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Jan 24, 2004 2:43 pm    Post subject: Re: typedef private or public ? Reply with quote

In message <d434bda2f2f07144a33434fdfcd4ddba.26421 (AT) mygate (DOT) mailgate.org>,
Aman <aman (AT) techie (DOT) com> writes
Quote:
The following piece of code works just fine .
even though typedef int T has been declared private , easily used in
main.

No it does not. Any compiler running in conforming mode should issue a
diagnostic. If yours does not it is not working correctly.

Quote:
(I am assuming it's because the name T is visible in main too - correct
?)
Does it make a difference (in terms of good design ) if I make it
private or public ?

Yes. If it is private you cannot use it outside the scope of the class,
and A::T is not enough to change that.

Quote:

#include class A {
private :
typedef int T;
} ;
int main(){
A::T i ;
i = 17 ;
cout << i << endl ;


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Aman Angrish
Guest





PostPosted: Tue Jan 27, 2004 11:02 am    Post subject: Re: typedef private or public ? Reply with quote

hmm... that's interesting .
I am using g++ 2.95.2 on SunOS 5.8.

The reason I asked was because I was tinkering with the difference
between visibility and accessibility .
got from Herb sutter article in cuj- (Mostly)private .

"A private member is visible to all code that sees the class's
definition. This means that ... it participates in name lookup and
overload resolution and so can make calls invalid or ambiguous
even though it itself could never be called."

after which I was wondering that it might be that the typedef holds
good for the rest of main() because the class is visible .

would that be correct ? or is it still "bad compiler" explanation.
I'm kinda confused now.

Quote:
#include class A {
private :
typedef int T;
} ;
int main(){
A::T i ;
i = 17 ;
cout << i << endl ;





--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Wed Jan 28, 2004 9:39 am    Post subject: Re: typedef private or public ? Reply with quote

Aman Angrish wrote:
Quote:
hmm... that's interesting .
I am using g++ 2.95.2 on SunOS 5.8.

The reason I asked was because I was tinkering with the difference
between visibility and accessibility .
got from Herb sutter article in cuj- (Mostly)private .

"A private member is visible to all code that sees the class's
definition. This means that ... it participates in name lookup and
overload resolution and so can make calls invalid or ambiguous
even though it itself could never be called."

after which I was wondering that it might be that the typedef holds
good for the rest of main() because the class is visible .

would that be correct ? or is it still "bad compiler" explanation.
I'm kinda confused now.
snip


No, the compiler is wrong. The compiler should issue an error
message along the lines of "A::T is private". If T was not visible
outside the class it should say something like "A::T is not defined".

The accessibility/visibility difference is particularly important
when it comes to overload resolution. Example:

class A
{
public:
static void f(double);
private:
static void f(int);
};

int main()
{
A::f(2);
}

In main(), A::f is resolved to mean A::f(int) based on the argument
given; the accessibility check is done after this and results in an
error. If private functions were invisible and so excluded from
overload resolution then it would be resolved to mean A::f(double).

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Jan 28, 2004 8:23 pm    Post subject: Re: typedef private or public ? Reply with quote

"Aman Angrish" <aman (AT) techie (DOT) com> wrote

Quote:
hmm... that's interesting .
I am using g++ 2.95.2 on SunOS 5.8.

The reason I asked was because I was tinkering with the difference
between visibility and accessibility .
got from Herb sutter article in cuj- (Mostly)private .

"A private member is visible to all code that sees the class's
definition. This means that ... it participates in name lookup and
overload resolution and so can make calls invalid or ambiguous
even though it itself could never be called."

after which I was wondering that it might be that the typedef holds
good for the rest of main() because the class is visible .

I think that the explination is fairly simple. Name lookup, and if
relevant, overload resolution, take place without regard to access. The
result is that the name resolves to a single declaration. Access to
that declaration is checked -- if you don't have rights, the program is
in error.

The fact that the name is declared by means of a typedef is irrelevant.
The fact that what the name means is publicly accessible is irrelevant;
access is checked on the declaration of the symbol actually used, and
not on what it means.

G++ pre 3.0 is notoriously weak on enforcing access controls. This is
an error (and was recognized as such by the g++ team).

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ 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.