Lemonbonbon
Lemonbonbon

Reputation: 738

Wrong use of forward declaration?

Sometimes I use foreward declaration for nested classes:

class A;

class B
{
  private:
    A* object_A;
  public:
    B(){}
};

The question: What happens if I now use forward declaration of class B (B is declared and defined at this point) for the useage in class C? Does this cause any problems with class B, because it is defined (with implementation of methods etc.) but is used with forward declaration for class C? Does the syntax class B; in the following code-snippet overwrites somehow the previous declared, defined and implemented class B?

class B;

class C
{
  private:
    B* object_B;
  public:
    C(){}
};

Upvotes: 1

Views: 128

Answers (1)

Mahesh
Mahesh

Reputation: 34615

Does the syntax class B; in the following code-snippet overwrites somehow the previous declared, defined and implemented class B?

Forward declarations are not about overwriting. It is just giving hint to the compiler that the type definition is implemented somewhere. For user defined pointer types, compiler does not require the definition but needs to know what is the type of the object.

Upvotes: 2

Related Questions