Forward declarations and inlining

J

jimmy

Hi,

I have circular dependancies between a pair of classes; Chicken and
Egg. I changed it to this;

_Chicken.h_
Class Egg;

_Egg.h_
#include "Chicken.h"

This worked fine until I tried to move some functions from Chicken.cpp
to Chicken.h for inlining. The problem is that Chicken.h doesn't have
the Egg's complete interface, only the Class declaration.

Is there a solution for this? Is it possible to forward declare the
functions I need?

-Jimmy
 
J

John Carson

jimmy said:
Hi,

I have circular dependancies between a pair of classes; Chicken and
Egg. I changed it to this;

_Chicken.h_
Class Egg;

_Egg.h_
#include "Chicken.h"

This worked fine until I tried to move some functions from Chicken.cpp
to Chicken.h for inlining. The problem is that Chicken.h doesn't have
the Egg's complete interface, only the Class declaration.

Is there a solution for this? Is it possible to forward declare the
functions I need?

The solution is to do all of the declaring first before you start defining,
e.g.,

Chicken_and_Egg.h

class Egg;

class Chicken
{
public:
void foo(Egg* ptr);
};

class Egg
{
public:
void foo(Chicken* ptr);
};

inline void Chicken::foo(Egg *ptr)
{
// stuff
}

inline void Egg::foo(Chicken *ptr)
{
// stuff
}

You could split this into multiple files if you wanted, but I think you will
need to have more than two header files, e.g.,

-------------------------------
Chicken0.h

class Egg;
class Chicken
{
public:
void foo(Egg* ptr);
};
----------------------------------
Egg0.h

class Chicken;
class Egg
{
public:
void foo(Chicken* ptr);
};
-------------------------------------
Chicken.h

#include "Chicken0.h"
#include "Egg0.h"

inline void Chicken::foo(Egg *ptr)
{
// stuff
}
---------------------------------------
Egg.h

#include "Chicken0.h"
#include "Egg0.h"

inline void Egg::foo(Chicken *ptr)
{
// stuff
}
 
D

David White

jimmy said:
Hi,

I have circular dependancies between a pair of classes; Chicken and
Egg. I changed it to this;

_Chicken.h_
Class Egg;

_Egg.h_
#include "Chicken.h"

This worked fine until I tried to move some functions from Chicken.cpp
to Chicken.h for inlining. The problem is that Chicken.h doesn't have
the Egg's complete interface, only the Class declaration.

Is there a solution for this? Is it possible to forward declare the
functions I need?

You can put the functions you want to inline into another file. Example:

// Egg.h
class Chicken;

class Egg
{
public:
Egg(Chicken &mother_) : mother(mother_) {}
void Hatch();
private:
Chicken &mother;
};

// Chicken.h
#include <list>
#include "Egg.h"

class Chicken
{
friend class Egg;
public:
Egg &LayEgg() {
eggs.push_front(Egg(*this));
return eggs.front(); }
private:
void EggHatched() { eggs.pop_back(); }
std::list<Egg> eggs;
};

// Egg.inl
inline void Egg::Hatch()
{
mother.EggHatched();
}

// Main.cpp
#include "Chicken.h"
#include "Egg.inl"

int main()
{
Chicken chicken;
Egg &egg = chicken.LayEgg();
egg.Hatch();
}

DW
 
J

John Carson

For brevity, I omitted include guards (to prevent a file being #included
multiple times) but naturally these should be incorporated into the header
file, as with every header file.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top