typedef private public

A

Aman

does it matter if a typedef is declared under public or private ?
does any one (public or private ) constitute better design ?
 
J

Jonathan Turkanis

Aman said:
does it matter if a typedef is declared under public or private ?
does any one (public or private ) constitute better design ?

The access specifiers private, protected and public affect the
visibility of typedefs just as they do other members. Sometimes
typedefs should be public, because they represent important types
associated with the interface of a class, such as the iterator types
of a container. (Of course, these don't have to be typedefs, but often
are.)

On the other hand, sometimes typedefs really are an implementation
detail which should be hidden from the end-user.

class Scrollbar : public Widget, private
ImplementationDetail<Scrollbar, std::string, 78, 47> {
private:
typedef ImplementationDetail<Scrollbar, std::string, 78, 47>
my_base;
public:
Scrollbar(int width) : my_base(width) { }
/* etc */
}

Jonathan
 
A

Aman

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

Rolf Magnus

Aman said:
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.
(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.
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.
#include <iostream>
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.
 
F

Falk =?iso-8859-1?Q?Tannh=E4user?=

Aman said:
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?
#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
<http://www.comeaucomputing.com/tryitout>, not
only because of the missing "std::" for "cout"
and "endl" but also because of the inaccessible
typedef.

Falk
 
F

Francis Glassborow

In message said:
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.
(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.
 
A

Aman Angrish

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

Ben Hutchings

Aman said:
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).
 
K

kanze

Aman Angrish said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top