gigi gigi - 4 years ago 73
C++ Question

Sentence in "c++ primer" that I don't understand

in the book "C++ primer" there is a section about class declarations and definitions. I don't understand everything about this sentence :

data members can be specified to be of a class type only if the class has been defined.

I don't understand the logic behind this sentence. How do you specify a data member to be of a class type, what does this action mean?

Answer Source

It means, for declaration of a non-static class data member of class type T, T is required to be complete.

(In general, when the size and layout of T must be known.)


class foo;    // forward declaration
class bar {
    foo f;    // error; foo is incomplete

On the other hand,

class foo {}; // definition
class bar {
    foo f;    // fine; foo is complete
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download