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:isplayClassName(ClassOne*);
};
#endif
#ifndef TWO_H
#define TWO_H
#include "one.h"
class ClassOne;
class ClassTwo
{
public:
void DisplayClassName(ClassOne*);
};
#endif
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:isplayClassName(ClassOne*);
};
#endif
#ifndef TWO_H
#define TWO_H
#include "one.h"
class ClassOne;
class ClassTwo
{
public:
void DisplayClassName(ClassOne*);
};
#endif