views:

296

answers:

3

Hi,

I am looking for a nice book, reference material which deals with forward declaration of classes esp. when sources are in multiple directories, eg. class A in dirA is forward declared in class B in dirB ? How is this done ?

Also, any material for template issues, advanced uses and instantation problems, highly appreicated ?

Thanks.

+1  A: 

if they are in parallel directories you can include it like

#include "../dirB/B.h"

but in header you just call this line for forward decleration

class B;


instead of this, you can seperate your include directories and source directories.

so then you can show include directory as this directory and you can add header by calling

#include "dirB/B.h"

since you will make a forward decleration on header, it wont be problem.

ufukgun
Don't use .. in includes.
Bruno Martinez
So, if my class B is in dirB and I am doing forward declaration and not include, is it the job of compiler to look where it is defined ?Is there any chance/possiblity for two classes to have same name, if the project is very large ? What will happen in this case ?
seg.server.fault
The forward declare means 'there's this type, but I haven't defined it yet'. It doesn't attempt to look it up. If you have two identically named types in the same scope you will get a compilation error.
CiscoIPPhone
Two classes can have the same name but they would have to be in different namespaces. Otherwise you would get a serious number of linker and compiler errors.
erelender
+1  A: 

Generally speaking you can forward declare in headers as a means of avoiding full includes or as a way to enable circular referencing (bad). You can use a forward declared type by pointer or reference or return type only.

Large-Scale C++ Software Design by John Lakos (book review here) addresses physical design (files) and logical design and how they relate to software components (which aren't always 1:1 with classes).

CiscoIPPhone
+3  A: 

Forward declarations have nothing to do with the directory structure of your project. You can forward declare something even not existing in your project. They are mostly used to resolve cyclic references between classes and to speed up compilation when the complete class declaration is not necessary, and the corresponding #include can be replaced with a forward declaration.

To determine when a forward declaration is sufficient, the sizeof() query can usually answer the question. For example,

class Wheel;

class Car
{
    Wheel wheels[4];
};

In this declaration, a forward declaration cannot be used since the compiler cannot determine the size of a Car: it doesn't know how much data the wheels contain. In other words, sizeof(Car) is unknown.

Also regarding templates, forward declared classes cannot be used as template parameters if the template class contains data members of the template parameter (but their pointers can be). For instance,

template<class T> class pointer
{
    T *ptr;
};

class Test;
pointer<Test> testpointer;

is legal but

std::vector<Test> testvector will not compile.

Because of the aforementioned limitations, forward declared classes are generally used as pointers or references.

I don't know if there's a book on the subject but you can see this section on c++ faq lite.

Can
I was just about to +1 this until I read: "forward declared classes cannot be used as template parameters". This is not true, can you give an exmaple of what you mean? We may then be able to phrase that sentence they way you really mean it. Also forward declared classes can be references as well as pointers.
Richard Corden
Richard, thanks for the response, I was in a hurry and obviously missed some points. What I said about template parameters is wrong, and I forgot about references.
Can