class declration

Discussion in 'C++' started by one2001boy@yahoo.com, Jul 25, 2004.

  1. Guest

    Hello,
    If I have a class defined as:

    class Test {
    public:
    .....
    }

    when I do the declaration,
    should I go with
    class Test t;
    or
    Test t;

    which does C++ standard recommend? it seems that both can be compiled
    and run successfully in C++, any pros and cons?

    thanks.
    , Jul 25, 2004
    #1
    1. Advertising

  2. On Sun, 25 Jul 2004 07:16:41 GMT,
    <> wrote:

    > Hello,
    > If I have a class defined as:
    >
    > class Test {
    > public:
    > ....
    > }
    >
    > when I do the declaration,
    > should I go with
    > class Test t;
    > or
    > Test t;
    >


    The second.

    > which does C++ standard recommend?


    I don't think the standard makes style recommendations.

    > it seems that both can be compiled and run successfully in C++, any pros
    > and cons?
    >


    I think they are completely identical, but the second has less typing and
    is what 99% of C++ programmers use.

    The first is similar to C, in C you have to say struct before a structure
    variable declaration. I guess C++ has retained this just for compatibility
    with C.

    struct X
    {
    };

    struct X x; // legal C and C++
    X y; // legal C++ only

    john
    John Harrison, Jul 25, 2004
    #2
    1. Advertising

  3. JKop Guest

    posted:

    > Hello,
    > If I have a class defined as:
    >
    > class Test {
    > public:
    > ....
    > }
    >
    > when I do the declaration,
    > should I go with
    > class Test t;
    > or
    > Test t;
    >
    > which does C++ standard recommend? it seems that both can

    be compiled
    > and run successfully in C++, any pros and cons?
    >
    > thanks.


    The idea is that a class, a struct, a union, an enum,
    should all just be treated as a "type". So if you can just
    write "int", why not just write "SomeClass".

    Anyway, you won't see much of:

    class Test t;

    You will see a lot of:

    Test t;

    As for which one you use: no-one gives a shit - they're
    both perfectly valid.

    -JKop
    JKop, Jul 25, 2004
    #3
    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. cyril
    Replies:
    2
    Views:
    3,838
    cyril
    Aug 25, 2004
  2. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    738
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    579
  5. Replies:
    3
    Views:
    442
    James Kanze
    Jun 12, 2008
Loading...

Share This Page