Can one class have two names?

Discussion in 'C++' started by JustSomeGuy, Dec 12, 2003.

  1. JustSomeGuy

    JustSomeGuy Guest

    class x,y
    {
    public:
    .....
    private:
    };
    JustSomeGuy, Dec 12, 2003
    #1
    1. Advertising

  2. JustSomeGuy wrote:
    > class x,y
    > {
    > public:
    > ....
    > private:
    > };
    >

    No. You can have one base class containing common
    stuff and have the two inherit from it:
    class base
    {
    // ...
    };

    class x
    : public base
    {
    // ...
    };

    class y
    : public base
    {
    // ...
    };

    Or you can have two instances of one class:
    class common
    {
    } x, y; // two variable of class common.

    Or you can throw the common stuff into
    a template.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Dec 12, 2003
    #2
    1. Advertising

  3. JustSomeGuy wrote:
    > class x,y
    > {
    > public:
    > ....
    > private:
    > };



    Thomas's post covers most of it.

    One thing he left out was typedefs.

    class x
    {
    public:
    .....
    private:
    };

    typedef x y;

    Now, x and y are exactly the same. However, y is really class x.

    i.e.

    void foo( y & );

    and

    void foo( x & );

    are exactly the same declaration.
    Gianni Mariani, Dec 12, 2003
    #3
  4. "Thomas Matthews" <> wrote...
    > JustSomeGuy wrote:
    > > class x,y
    > > {
    > > public:
    > > ....
    > > private:
    > > };
    > >

    > No. You can have one base class containing common
    > stuff and have the two inherit from it:
    > class base
    > {
    > // ...
    > };
    >
    > class x
    > : public base
    > {
    > // ...
    > };
    >
    > class y
    > : public base
    > {
    > // ...
    > };
    >
    > Or you can have two instances of one class:
    > class common
    > {
    > } x, y; // two variable of class common.
    >
    > Or you can throw the common stuff into
    > a template.


    Or you could use 'typedef' thereby creating a synonym for
    your class (hey, isn't a synonym a "second name"?) ....
    Victor Bazarov, Dec 12, 2003
    #4
  5. JustSomeGuy

    Adam Fineman Guest

    JustSomeGuy wrote:
    > class x,y
    > {
    > public:
    > ....
    > private:
    > };
    >


    No, you can't do that, but you could do this:

    class x { /* ... */ };

    typedef x y;

    - Adam

    --
    Reverse domain name to reply.
    Adam Fineman, Dec 12, 2003
    #5
  6. "Gianni Mariani" <> wrote in message
    news:brctkb$...
    > JustSomeGuy wrote:
    > > class x,y
    > > {
    > > public:
    > > ....
    > > private:
    > > };

    >
    >
    > Thomas's post covers most of it.
    >
    > One thing he left out was typedefs.
    >
    > class x
    > {
    > public:
    > ....
    > private:
    > };
    >
    > typedef x y;
    >
    > Now, x and y are exactly the same. However, y is really class x.


    They are not exactly the same.
    While you can declare x in a forward declaration as a class, you cannot do
    the same for y:

    class x; // ok
    class y; // error
    Eugene Alterman, Dec 12, 2003
    #6
  7. Eugene Alterman wrote:
    > "Gianni Mariani" <> wrote in message
    > news:brctkb$...
    >
    >>JustSomeGuy wrote:
    >>
    >>>class x,y
    >>>{
    >>>public:
    >>>....
    >>>private:
    >>>};

    >>
    >>
    >>Thomas's post covers most of it.
    >>
    >>One thing he left out was typedefs.
    >>
    >>class x
    >>{
    >>public:
    >>....
    >>private:
    >>};
    >>
    >>typedef x y;
    >>
    >>Now, x and y are exactly the same. However, y is really class x.

    >
    >
    > They are not exactly the same.
    > While you can declare x in a forward declaration as a class,
    > you cannot do the same for y:


    Sure you can:

    class x; // OK
    typedef x y; // also OK
    E. Robert Tisdale, Dec 12, 2003
    #7
  8. JustSomeGuy

    David White Guest

    "Eugene Alterman" <> wrote in message
    news:QpoCb.36164$...
    > "Gianni Mariani" <> wrote in message
    > news:brctkb$...
    > > typedef x y;
    > >
    > > Now, x and y are exactly the same. However, y is really class x.

    >
    > They are not exactly the same.
    > While you can declare x in a forward declaration as a class, you cannot do
    > the same for y:
    >
    > class x; // ok
    > class y; // error


    Yes, and this can be a pain sometimes. It would be nice if the language did not take 'class y'
    literally (a little like 'class' as a template argument), and instead as just 'some type' for
    the purpose of declaring pointers or references, or whatever the forward declaration is for. If
    'y' turns out to be some incompatible type, such as a reference, the compiler can catch it
    later, just as does now if 'y' turns out to be a struct rather than a class.

    DW
    David White, Dec 12, 2003
    #8
  9. JustSomeGuy

    jeffc Guest

    "JustSomeGuy" <> wrote in message
    news:brcrgr$19gu$...
    > class x,y
    > {
    > public:
    > ....
    > private:
    > };


    Not like that. But you can just use typedef (without knowing exactly what
    you're trying to do.)
    jeffc, Dec 15, 2003
    #9
  10. "JustSomeGuy" <> wrote in message
    news:brcrgr$19gu$...
    > class x,y
    > {
    > public:
    > ....
    > private:
    > };


    Yes:

    typedef class x
    {
    public:
    .....
    private:
    } y;

    Cheers!

    - Risto -
    Risto Lankinen, Dec 16, 2003
    #10
    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. Weddick

    One Signal Two Names

    Weddick, Sep 17, 2005, in forum: VHDL
    Replies:
    5
    Views:
    469
    Mike Treseler
    Sep 18, 2005
  2. news.frontiernet.net
    Replies:
    6
    Views:
    1,118
    news.frontiernet.net
    Apr 16, 2004
  3. Tom
    Replies:
    1
    Views:
    411
    Michiel Salters
    Jul 4, 2003
  4. Ares Lagae
    Replies:
    8
    Views:
    446
    Ares Lagae
    Sep 24, 2004
  5. mark4asp
    Replies:
    2
    Views:
    617
    Jasbird
    Nov 11, 2006
Loading...

Share This Page