473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Forward declarations and inlining

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

Jul 23 '05 #1
3 1200
"jimmy" <ji**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
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
}

--
John Carson

Jul 23 '05 #2
"jimmy" <ji**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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
Jul 23 '05 #3
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.

--
John Carson

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
12
by: fox | last post by:
How do I (portably) make a forward reference to a static (I mean file-scope) variable? I've been using "extern" for years, for example: extern int x; int foo(void) { return x++; }
2
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
10
by: collection60 | last post by:
Will C++ ever have Java-like multiple level compiling? So that we can dump the old model of having to first declare then write the code? I find the declaration wastes so much time in coding....
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: Rune Allnor | last post by:
Hi all. I have these classes, implemented as templates in header files. Right now I have one file per class, where both the declarations and implementations of one class are located in each...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.