Sunday, July 6, 2008

C++: What's the difference between class and struct?

What's the difference between class and struct?

A beginner would say:

"There are a lot of differences, class is a C++ element and struct is a C one. We can have functions, virtual functions, inheritance, different access modifiers private, protected, public in a class but probably not in a struct. A struct generally holds member variables only."

(Just to clarify, the above statement is completely wrong.)

Intermediate and most advanced C++ programmers would say:

"class and struct do not have any difference, except the fact that class members are by default private and struct members are by default public. The keywords class and struct are interchangeable."

The above statement is correct to some extent and would satisfy most questioners, however, there are some special cases where class and struct are not interchangeable.

Consider the following code which compiles well in a C++ complier:

template <class T>
void fn()

Now replace the keyword 'class' with 'struct' and compile it..

template <struct T>
void fn()

It gives a compiler error! According to MSDN's definition of the template keyword:

"The template-parameter-list is a comma-separated list of template parameters, which may be types (in the form class identifier, typename identifier, or template < template-parameter-list > class identifier) or non-type parameters to be used in the template body."

So the template parameter list doesn't take the keyword 'struct'!

The template mechanism itself was introduced in later phase of the C++ evolution, some years later than the first version of C++. The 'struct' keyword was probably ignored when defining template which was considered to be an advance feature of C++.

"Another reasonable use of the C struct in C++, then, is when you want to pass all or part of a complex class object to a C function. This struct declaration serves to encapsulate that data and guarantees a compatible C storage layout. This guarantee, however, is maintained only under composition." said Stanley B. Lippman, author of several C++ books.


luc said...

if some desperate user wants to use the struct keyword in a template declaration he can.

Surprised to hear that..??

just a little trick needed..

so here goes : :)

#define struct class

template < struct T> struct mystruct

this code will compile in vc6..

clever.. huh.. king?

Mohammad Kaisar Ul Haque said...

haha.. I'll call that desperate programmer an evil :P.

Ronak Patel said...

good article kaisar. Nice to learn that you can have class templates but not struct templates. Nice finding. Over all it clearly answers the question.

Mohammad Kaisar Ul Haque said...

Thanks Ronak.

wifreak said...

@Ronac, just to prevent confusion, nothing in C++ prevents you from declaring struct templates, like

template < class T >
struct A

Expression < class T > has nothing to do with C++ class, it just declares "type" variable with a name T. BTW, < typename T > should work the same way. When you instantiate A, you assign T to any primitive type, structure or a class.

In the end, default member visibility seems to be the only difference between class and struct in C++

Anonymous said...

Can u provide me in a tabular form ?hope to receive from u soon

Mohammad Kaisar Ul Haque said...

@Anonymous: Sorry, I wish I had enough time.