51

Possible Duplicates:
C/C++ Struct vs Class
What are POD types in C++?

In the C++ In a Nutshell book, in chapter 6: classes, unders Access specifiers, mentioned the following:

In a class definition, the default access for members and base classes is private. In a struct definition, the default is public. That is the only difference between a class and a struct, although by convention, some programmers use struct only for POD classes and use class for all other classes.

My questions here are:

  • Isn't there another difference between classes and structs in that structs don't hold functions and just hold data?
  • What are POD classes? And, what is meant by all other classes here? Are there then special classes?
3

5 Answers 5

94

The other difference is that

template<class T> ...

is allowed, but

template<struct T> ...

is not.

4
  • 21
    +1 for unique approach
    – Grozz
    Jan 25, 2011 at 11:04
  • 16
    The official way to use template is template<typename T>, template<class T> is kinda misleading, it's there because of legacy.
    – Peter Lee
    Sep 18, 2013 at 16:08
  • then add another difference between int and class too..!! template<class T> ... works but template<int T> ... not Jan 16, 2016 at 15:03
  • @UnKnown, of course that works, we just call the integers I, J, K, L for historical reasons. Jan 16, 2016 at 20:19
40

You could prove to yourself that there is no other difference by trying to define a function in a struct. I remember even my college professor who was teaching about structs and classes in C++ was surprised to learn this (after being corrected by a student). I believe it, though. It was kind of amusing. The professor kept saying what the differences were and the student kept saying "actually you can do that in a struct too". Finally the prof. asked "OK, what is the difference" and the student informed him that the only difference was the default accessibility of members.

A quick Google search suggests that POD stands for "Plain Old Data".

3
  • 9
    A bad time for that teacher, and no doubt :)
    – Alex
    Jul 22, 2014 at 14:52
  • That teacher was trying to explore/learned the difference between struct and class in C not C++ because in C we can't add functions in struct. Jan 16, 2016 at 15:05
  • C doesnt even have classes :P people just think structs in C++ can't have methods because they think of C++ as a superset of C which leads to the incorrect assumption that C++ structs are the same as C structs. Jun 19, 2017 at 2:31
8

POD classes are Plain-Old data classes that have only data members and nothing else. There are a few questions on stackoverflow about the same. Find one here.

Also, you can have functions as members of structs in C++ but not in C. You need to have pointers to functions as members in structs in C.

3

Ok, POD means plain old data. That usually refers to structs without any methods because these types are then used to structure multiple data that belong together.

As for structs not having methods: I have seen more than once that a struct had methods, and I don't feel that this would be unnatural.

2

1) It is the only difference in C++.

2) POD: plain old data Other classes -> not POD