declaration vs definition w/classes

  • Thread starter xllx.relient.xllx
  • Start date
X

xllx.relient.xllx

I have a few quetions about definitions vs declarations concerning
defined types (user defined), specifically classes. From what I
understand, a class contained in a header file is a definition as a
whole, but the contents a.k.a members, are the declarations (non-inline
functions). What I'm trying to understand, is, is storage allocated or
not allocated for them at this phase? Next, the members implemented in
a .cpp file, for the class, are their storage allocated for them during
compilation, or is this done in the class definition (header file)? I'm
trying to figure out a problem (contained below) but I first need these
questions cleared for me, Thanks:

The following compiles fine when in the "two.h" file, a forward
declaration (class ClassOne;), is used, without #include "one.h", but,
when I try removing the forward declaration and this time only #include
"one.h", I get 2 errors. I initially thought that both were merely the
same, for one because a forward delcaration is an incomplete type, and
resolves during link time, so I see how there's no problem there.
However, I thought that including the header file instead of the
forward declaration would bring about the same error-free compilation
because the definition, which is the class, is being inserted at the
top, where the include directive is at, and therefore, a definition =
better than delclaration because it is "not" an incomplete type and
rather, it has been allocated storage. Where am I going wrong?

#ifndef ONE_H
#define ONE_H
#include "two.h"

class ClassOne
{

public:
ClassOne();
friend void ClassTwo::DisplayClassName(ClassOne*);
};
#endif


#ifndef TWO_H
#define TWO_H
#include "one.h"

class ClassOne;
class ClassTwo
{

public:
void DisplayClassName(ClassOne*);
};
#endif
 
J

Jakob Bieling

I have a few quetions about definitions vs declarations concerning
defined types (user defined), specifically classes. From what I
understand, a class contained in a header file is a definition as a
whole, but the contents a.k.a members, are the declarations
(non-inline functions). What I'm trying to understand, is, is storage
allocated or not allocated for them at this phase? Next, the members
implemented in a .cpp file, for the class, are their storage
allocated for them during compilation, or is this done in the class
definition (header file)? I'm trying to figure out a problem
(contained below) but I first need these questions cleared for me,

You do not need to know that. And acutally you cannot, because every
compiler will allocate storage whenever it wants to. You do need to know
tho, when an incomplete type is sufficient and when it is not.
The following compiles fine when in the "two.h" file, a forward
declaration (class ClassOne;), is used, without #include "one.h", but,
when I try removing the forward declaration and this time only
#include "one.h", I get 2 errors. I initially thought that both were
merely the same, for one because a forward delcaration is an
incomplete type, and resolves during link time, so I see how there's
no problem there. However, I thought that including the header file
instead of the forward declaration would bring about the same
error-free compilation because the definition, which is the class, is
being inserted at the top, where the include directive is at, and
therefore, a definition = better than delclaration because it is
"not" an incomplete type and rather, it has been allocated storage.
Where am I going wrong?

#ifndef ONE_H
#define ONE_H
#include "two.h"

class ClassOne
{

public:
ClassOne();
friend void ClassTwo::DisplayClassName(ClassOne*);

At this point you need the definition of the class. That is, the
compiler needs to know, that this function exists in 'ClassTwo'.
};
#endif


#ifndef TWO_H
#define TWO_H
#include "one.h"

class ClassOne;
class ClassTwo
{

public:
void DisplayClassName(ClassOne*);

Here the class declaration is sufficient, because you are not using
the definition. All the compiler needs to know is, that 'ClassOne'
exists.
};
#endif

Now when you include 'one.h' in 'two.h', take a look at what is
actually happening:

#ifndef TWO_H
#define TWO_H

#include "one.h" --> expands to:
#ifndef ONE_H
#define ONE_H
#include "two.h" --> expands to:
#ifndef TWO_H
// TWO_H was already defined,
// so it's not included again
#endif


class ClassOne
{

public:
ClassOne();
friend void ClassTwo::DisplayClassName(ClassOne*);
};
#endif


class ClassTwo
{

public:
void DisplayClassName(ClassOne*);
};
#endif

You will see that 'ClassOne' needs the definition of 'ClassTwo',
which is given at a later point. Too late for it to compile.

hth
 
B

benben

I have a few quetions about definitions vs declarations concerning
defined types (user defined), specifically classes. From what I
understand, a class contained in a header file is a definition as a
whole, but the contents a.k.a members, are the declarations (non-inline
functions). What I'm trying to understand, is, is storage allocated or
not allocated for them at this phase? Next, the members implemented in
a .cpp file, for the class, are their storage allocated for them during
compilation, or is this done in the class definition (header file)? I'm
trying to figure out a problem (contained below) but I first need these
questions cleared for me, Thanks:

What do you mean by "storage"? Please be more specific.

Generally, if you have a class declaration, then you can refer to a
pointer or reference of that class:

class Line;

Line& foo();

Line* pline; // OK
Line& refline = foo(); // OK

Line line; // Error: unknown sizeof(Line);

With a full definition of the class you will be able to use the object
of that class:

class Line{
public:
Point pt1, pt2;

void bar();
};

Line line; // Ok
line.pt1 = Point(12, 24); // Ok

And finally, we need the definition of the bar to link successfully.

Regards
Ben
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top