class declration

O

one2001boy

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

John Harrison

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
 
J

JKop

(e-mail address removed) 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
 

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,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top