Connecting Tech Pros Worldwide Forums | Help | Site Map

class declration

one2001boy@yahoo.com
Guest
 
Posts: n/a
#1: Jul 22 '05
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.

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: class declration


On Sun, 25 Jul 2004 07:16:41 GMT, one2001boy@yahoo.com
<one2001boy@yahoo.com> wrote:
[color=blue]
> 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;
>[/color]

The second.
[color=blue]
> which does C++ standard recommend?[/color]

I don't think the standard makes style recommendations.
[color=blue]
> it seems that both can be compiled and run successfully in C++, any pros
> and cons?
>[/color]

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
JKop
Guest
 
Posts: n/a
#3: Jul 22 '05

re: class declration


one2001boy@yahoo.com posted:
[color=blue]
> 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[/color]
be compiled[color=blue]
> and run successfully in C++, any pros and cons?
>
> thanks.[/color]

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
Closed Thread