answersLogoWhite

0

Which came first, the chicken or the egg? The answer is, of course, the egg. After all, birds evolved long after egg-laying reptiles, so eggs had to have come first. But what exactly does that have to do with forward declarations? Well, everything, as it turns out. Forward declarations are essential whenever two classes depend on each other.


As you know, in C++ you must define a type before you can use it. So let's define a simple chicken and egg:

#include


class Chicken

{

public:

Chicken(Chicken* parent=0):m_pParent(parent){}

Egg* lay_egg();

private:

Chicken* m_pParent;

};


class Egg

{

public:

Egg(Chicken* parent=0): m_pParent(parent){}

Chicken* hatch();

private:

Chicken* m_pParent;

};


Egg* Chicken::lay_egg()

{

return(new Egg(this));

}


Chicken* Egg::hatch()

{

return(new Chicken(m_pParent));

}


int main()

{

Chicken chicken;

Egg* egg = chicken.lay_egg();

Chicken* chick = egg->hatch();

Egg* egg2 = chick->lay_egg();

delete(egg2);

egg2=0;

delete(chick);

chick=0;

delete(egg);

egg=0;

return(0);

}

Straight away there's a problem. The compiler won't allow this because our chicken lays eggs but the definition of an egg appears after the definition of a chicken. Ah, but of course -- eggs came first! So let's swap the definitions around:

#include


class Egg

{

public:

Egg(Chicken* parent=0): m_pParent(parent){}

Chicken* hatch();

private:

Chicken* m_pParent;

};


class Chicken

{

public:

Chicken(Chicken* parent=0):m_pParent(parent){}

Egg* lay_egg();

private:

Chicken* m_pParent;

};


Egg* Chicken::lay_egg()

{

return(new Egg(this));

}


Chicken* Egg::hatch()

{

return(new Chicken(m_pParent));

}


int main()

{

Chicken chicken;

Egg* egg = chicken.lay_egg();

Chicken* chick = egg->hatch();

Egg* egg2 = chick->lay_egg();

delete(egg2);

egg2=0;

delete(chick);

chick=0;

delete(egg);

egg=0;

return(0);

}

Hmm. The compiler's still not happy. Our eggs need to hatch chickens but, again, the definition of a chicken now appears after the definition of an egg. We seem to have a catch-22 situation. No matter which order we define them, we simply cannot emulate a simple chicken and an egg.


The answer is, you guessed it, to use a forward declaration:

#include


class Chicken; // forward declaration!


class Egg

{

public:

Egg(Chicken* parent=0): m_pParent(parent){}

Chicken* hatch();

private:

Chicken* m_pParent;

};


class Chicken

{

public:

Chicken(Chicken* parent=0):m_pParent(parent){}

Egg* lay_egg();

private:

Chicken* m_pParent;

};


Egg* Chicken::lay_egg()

{

return(new Egg(this));

}


Chicken* Egg::hatch()

{

return(new Chicken(m_pParent));

}


int main()

{

Chicken chicken;

Egg* egg = chicken.lay_egg();

Chicken* chick = egg->hatch();

Egg* egg2 = chick->lay_egg();

delete(egg2);

egg2=0;

delete(chick);

chick=0;

delete(egg);

egg=0;

return(0);

}

Now the code compiles!


The forward declaration simply acts as a sort of place-holder. We're just telling the compiler that although we aren't quite ready to define a chicken, one will be defined at some point -- it may even be in a completely different file. But that is enough to appease the compiler, it can simply fill in the blanks when our chicken is fully defined.


This type of scenario crops up quite a lot, especially when working with parent and child classes that must depend on each other, just like our chicken and egg. However, we normally design our classes using separate source files each with their own header file, and that would then make it impossible for our chicken and egg header's to include each other's header. Instead, we must use forward declarations in the headers, and place the corresponding #include directives in the source files.

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

In c plus plus Who tells the compiler that a specific class will be declared later in the program?

A forward declaration. However forward declarations can only be used when the class is used as a pointer or reference prior to its definition, otherwise it must be defined before it is used. class A; // forward declaration class B { A& data; // reference to class that has yet to be defined }; class A {}; // definition


What do you mean by forward declarations in c plus plus?

A forward declaration is simply a declaration of an identifier that will be defined at a later point during compilation. It is quite common to separate declarations from their definitions using header files (.h) and source files (.cpp). The header essentially contains all the forward declarations of all the definitions contained in the source file. It is also common practice to use forward declarations when a header contains pointers to a type, rather than references. If it includes references, then you must include the header file for those references, but if they are pointers then you only need a forward declaration of the type (before it is used) and the actual header that contains the full declaration can be included in the source file instead.


What are the two parts of class specification in c plus plus programming?

There are more than two, but the class name and interface would be the minimum specification for any class. The interface can be further divided into public, protected and private access, each of which may contain member methods and/or member variables, which may themselves be static and/or non-static. The last part of the specification are the friend declarations, if required.


In c plus plus are local declarations visible to the function in a program?

If you declare a variable inside of any fuction (except main) it will not be available to other functions.


Interface in C plus plus?

Unless by "interface" you mean a user interface...C++ does not have interfaces per se, at least not in the same sense as, say, Java. In a C++ class, a function declared pure virtual makes the class non-instantiable and forces derived classes that want to be instantiable to provide an implementation. This has exactly the same effect as the interface concept of Java. So in C++, interface is just a synonym for abstract base class.

Related Questions

In c plus plus Who tells the compiler that a specific class will be declared later in the program?

A forward declaration. However forward declarations can only be used when the class is used as a pointer or reference prior to its definition, otherwise it must be defined before it is used. class A; // forward declaration class B { A& data; // reference to class that has yet to be defined }; class A {}; // definition


What do you mean by forward declarations in c plus plus?

A forward declaration is simply a declaration of an identifier that will be defined at a later point during compilation. It is quite common to separate declarations from their definitions using header files (.h) and source files (.cpp). The header essentially contains all the forward declarations of all the definitions contained in the source file. It is also common practice to use forward declarations when a header contains pointers to a type, rather than references. If it includes references, then you must include the header file for those references, but if they are pointers then you only need a forward declaration of the type (before it is used) and the actual header that contains the full declaration can be included in the source file instead.


How do you rectify braces expected error in c plus plus language?

This type of error indicates you've omitted braces where braces were expected. For instance, class declarations must be enclosed within curly braces, as must function definitions.


How do you begin a page of C plus plus coding?

It depends whether you are writing a header or a source file. Generally you will begin with the header, and this should always begin with a header guard: // file: my_header.hpp #ifndef _MY_HEADER_HPP_ #define _MY_HEADER_HPP_ //... // header code goes here // ... #endif _MY_HEADER_HPP_ Although the opening header guard should always be placed first, it's a good idea to precede the header guard with a multi-line comment briefly explaining the purpose of the header and its contents, as well as the author's contact details and any required copyright notifications. The header code will include any other required headers as well as any required forward declarations, followed by its own declarations. You may also include definitions for those declarations, however its generally best to keep implementation details separate from the declarations. The only exceptions are when declaring class templates, which must be completely defined in the header, or when defining implicit inline functions. The corresponding source file must include the header file, along with any other header files for its forward declarations, before defining the implementations of the header's undefined declarations. Other files that require those definitions need only include the corresponding header file.


What are the two parts of class specification in c plus plus programming?

There are more than two, but the class name and interface would be the minimum specification for any class. The interface can be further divided into public, protected and private access, each of which may contain member methods and/or member variables, which may themselves be static and/or non-static. The last part of the specification are the friend declarations, if required.


In c plus plus are local declarations visible to the function in a program?

If you declare a variable inside of any fuction (except main) it will not be available to other functions.


Can a c plus plus class be derived from a Java class?

No.


How do you create a class in C plus plus?

class class_name { private: data_members; public: member_functions; };


Interface in C plus plus?

Unless by "interface" you mean a user interface...C++ does not have interfaces per se, at least not in the same sense as, say, Java. In a C++ class, a function declared pure virtual makes the class non-instantiable and forces derived classes that want to be instantiable to provide an implementation. This has exactly the same effect as the interface concept of Java. So in C++, interface is just a synonym for abstract base class.


What is nested class in c plus plus?

s.


Structure in c plus plus?

Structures are the same as classes in C++. The only difference is that structure members are public by default while class members are private by default, but both define a class of object and both are initialised via a class constructor initialisation list. struct my_object { my_object(const int data): m_data(data) {} private: int m_data; }; The above struct is no different to the following class: class my_object { int m_data; public: my_object(const int data): m_data(data) {} };


What is the unit of programming in c plus plus A. Function B. class C. object D. Attribute?

B. Class.