typedef private public

Discussion in 'C++' started by Aman, Jan 21, 2004.

  1. Aman

    Aman Guest

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


    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
    Aman, Jan 21, 2004
    #1
    1. Advertising

  2. "Aman" <> wrote in message
    news:...
    > 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
    Jonathan Turkanis, Jan 21, 2004
    #2
    1. Advertising

  3. Aman

    Aman Guest

    Re: typedef private or public ?

    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! ]
    Aman, Jan 23, 2004
    #3
  4. Aman

    Rolf Magnus Guest

    Re: typedef private or public ?

    Aman wrote:

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


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Rolf Magnus, Jan 24, 2004
    #4
  5. Re: typedef private or public ?

    Aman wrote:
    >
    > 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

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Falk =?iso-8859-1?Q?Tannh=E4user?=, Jan 24, 2004
    #5
  6. Re: typedef private or public ?

    In message <>,
    Aman <> writes
    >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.

    >
    >#include <iostream>
    >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! ]
    Francis Glassborow, Jan 24, 2004
    #6
  7. Aman

    Aman Angrish Guest

    Re: typedef private or public ?

    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.

    > >#include <iostream>
    > >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! ]
    Aman Angrish, Jan 27, 2004
    #7
  8. Re: typedef private or public ?

    Aman Angrish wrote:
    > 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! ]
    Ben Hutchings, Jan 28, 2004
    #8
  9. Aman

    Guest

    Re: typedef private or public ?

    "Aman Angrish" <> wrote in message
    news:<>...
    > 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:
    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! ]
    , Jan 28, 2004
    #9
    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.

Share This Page