Green goblin
Green goblin

Reputation: 9996

structure vs class

Today, I was curious to find some of differences between a structure and a class, in C++. So, I found some of the differences:

  1. In a structure, by default members are public while private in class.
  2. Inheritance in case of a structure is public by default, while private in case of class.
  3. Classes can take part in templates, while structures cannot.

click here to see that a struct cannot be used in place of class in case of template. http://ideone.com/p5G57

template<struct T> void fun(T i)
{
    cout<<i<<endl;
}

int main()
{
    int i=10;
    fun<int>(i);
    return 0;
}

It gives the errors:

prog.cpp:4: error: ‘struct T’ is not a valid type for a template constant parameter
prog.cpp: In function ‘void fun(T)’:
prog.cpp:4: error: ‘i’ has incomplete type
prog.cpp:4: error: forward declaration of ‘struct T’
prog.cpp: In function ‘int main()’:
prog.cpp:12: error: no matching function for call to ‘fun(int&)’

However, if struct is replaced with class, it works perfectly. see here: http://ideone.com/K8bFn

Apart from these above differences, when I replace class with struct in my code, the code works perfectly without making any further changes.

Now, I want to know, are there more differences, that I am missing and I should know?

Upvotes: 14

Views: 4299

Answers (3)

Kushan
Kushan

Reputation: 10695

There are two main differences:

  • In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

  • Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.

Upvotes: 4

MikMik
MikMik

Reputation: 3466

A struct is just a class with all members public by default.

According to The C++ Programming Language (3rd ed.), section 10.2.8:

By definition, a struct is a class in which members are by default public; that is

struct s{ ...

is simply shorthand for

class s { public: ...

Then he goes on to say:

I usually prefer to use struct for classes that have all data public. I think of such classes as "not quite proper types, just data structures."

Edited per the comments:

In section 15.3.2 it says:

The access specifier for a base class can be left out. In that case, the base defaults to a private base for a class and a public base for a struct.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

There's no other difference, but the third one you specify isn't correct:

Class can take part in template while structures cannot.

In case of templates, the class keyword is just syntactic sugar, it doesn't mean the type has to be an actual class. Generally, programmers prefer typename for basic types and class for classes or structs, but that's just by convention.

Other than that, you can use both class and struct to specialize templates.

Upvotes: 27

Related Questions