class a and b of different namespace 'need each other'

Started by
4 comments, last by cozzie 10 years, 1 month ago

Hi,

I've been struggling quite a while to get 2 classes of different namespace in different header files, knowing each other. The only way I managed to solve this is having a forward declaration right before including the header of the other. I've pasted the code below. While doing this I have a few questions, I hope someone can clarify this;

1. When classes A and B (of different namespaces) need to know each other, why doesn't it work to forward declare them both ways around?

(even though they're only needed as a pointer or const ref)

2. Why does my forward declaration of CD3dscene work without 'being' in the d3drenderer namespace and the renderqueue fwd declaration doesn't work, unless I explicitly place it there, i.e. namespace Crealysm_renderer { class CRenderQueue; }. Fwd declaring "Crealysm_renderer::CRenderQueue' gives an error the Crealysm_renderer is not a known namespace.

Any ideas?


// d3dscene header

#ifndef CD3DSCENE_H
#define CD3DSCENE_H

class Crealysm_d3drenderer::CD3dscene;
#include "Crealysm_renderqueue.h"

namespace Crealysm_d3drenderer
{

class CD3dscene
{
// etc
	//	this only works if the fwd declaration is 'within' the Crealysm_renderer namespace
	//	bool TransformObjects(const Crealysm_renderer::CRenderQueue &pQueue);
	bool TransformObjects(const CRenderQueue &pQueue);
};
}
#endif

// renderqueue header

#ifndef CRENDERQUEUE_H
#define CRENDERQUEUE_H

class CRenderQueue;
#include "Crealysm_d3dscene.h"

namespace Crealysm_renderer
{
class CRenderQueue
{
// etc.
	bool Create(const Crealysm_d3drenderer::CD3dscene *pD3dscene, const int pLightingEffectId);			// changed
};
}
#endif

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement
I think both questions are different facets of the same issue: The compiler needs enough information to do the job.

As you mentioned, you can forward declare things inside a namespace with:

namespace foo { namespace bar { class a; } }

Now you can refer to it as: foo::bar::a. It provides enough information that it can properly mangle the name according to the implementation's name-mangling details.

While it would sometimes be convenient, the language grammar does not permit this kind of forward declaration:

class foo::bar::a; // Not valid in the grammar, but would be convenient for programmers.

You could find patches that allow non-standard extensions like this for gcc, but you would be very hard pressed to convince the industry as a whole to rewrite their compilers to accommodate the grammar change.

Once you have that, it means the compiler knows enough about it to make a pointer and to mangle the name properly to identify it. The declaration does not give enough information to deduce things like the size of the object, the offset of members, or other assorted details. This can happen when you pass by value or try to dereference the pointer and access a member, or otherwise need to know anything about the value of the unknown class. You need to make sure your code doesn't do any of that, even by accident, or the compiler will complain.

There is a commonly-used pattern that can help with this issue, called the "dependency inversion principle". Instead of relying on the actual object, you rely on an abstract interface or abstract base class, that the concrete class implements. This has a natural side effect that you cannot pass an abstract interface by value, you can only do it by pointer. When you write your implementation details, you won't be able to do any of the 'by value' work that requires inner knowledge of the target class like its size, so most circular reference problems will naturally vanish.

/Edit: To be more clear, dependency inversion does not necessarily mean using an interface or abstract base class. It means that instead of depending on the details, you depend on the abstraction. You don't care if they use that renderer or a different renderer, all you care is that when you call draw(), the right thing happens. In C++ this is frequently implemented through an ABC or an interface class, but there are additional ways to do it like the CRTP and SFINAE. All the code cares is that the abstraction works, not necessarily that a specific implementation is used.

You're circularly including the headers; while this is fine (because you are using inclusion guards) it's still pretty weird - at least to me.

Another "oddity" in your posted code snippet is your forward-declaring classes before including. This means that everywhere you #include the header, you'll need to also forward-declare the classes again.

Forward-including each class should work. I *think* it was just the namespace issue that was messing it up.

A better way of doing it, would be this:


// Crealysm_d3dscene.h header

#ifndef CD3DSCENE_H
#define CD3DSCENE_H

class CRenderQueue; //Just forward-declare it here. <<<<<<<<<

namespace Crealysm_d3drenderer
{
    class CD3dscene
    {
         bool TransformObjects(const CRenderQueue &pQueue);
    };
}
#endif

//=====================================================================

// Crealysm_renderer.h header

#ifndef CRENDERQUEUE_H
#define CRENDERQUEUE_H

namespace Crealysm_d3drenderer { class CD3dscene; } //Just forward-declare it here. <<<<<<<<<

namespace Crealysm_renderer
{
    class CRenderQueue
    {
        bool Create(const Crealysm_d3drenderer::CD3dscene *pD3dscene, const int pLightingEffectId);
    };
}
#endif

And only #include the headers in the source (.cpp) files.

Unless I'm missing something here, this should work fine for you, and what's more, is what most programmers are used to doing.

(most programmers = It's what I'm used to doing, and what I observe others do. tongue.png)

Thanks, you're absolute right on the namespaces issue, I've solved it now without the unnessary includes:


// header 1

#ifndef CD3DSCENE_H
#define CD3DSCENE_H

namespace Crealysm_IO { class CScene; };
namespace Crealysm_d3drenderer { class CD3dscene; };
namespace Crealysm_renderer { class CRenderQueue; };

// all three headers not included anymore

namespace Crealysm_d3drenderer
{
class CD3dscene
{
public:
	bool TransformObjects(const Crealysm_renderer::CRenderQueue &pQueue);
} // etc

// header 2

#ifndef CRENDERQUEUE_H
#define CRENDERQUEUE_H

#include <vector>

namespace Crealysm_d3drenderer { class CD3dcam; }
namespace Crealysm_renderer	{ class CRenderQueue; }

// etc.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

You should definitely give some thought to dependency inversion. Circular dependencies can very easily become problematic in the future. Unless there is some reason not to abstract out an interface, it would benefit you to do so.

Thanks, I fully agree that rethinking design keeping similar rules & principles in mind, is always a good thing once in a while.

In this case they currently 'need each other', for the following reason:

- D3D scene needs renderqueue pointer because the resulting visible renderable's ID's (after culling) are sent to the renderqueue

- Renderqueue needs pointer to D3D scene because it's the base that fills the queue

I've thought of other options, but in any case at some point some data from the D3Dscene needs to be transferred to the renderqueue. Theoretically I could do this using passing one or more standard types to a 'receiving' generic member function of renderqueue. That way the interdependency would be gone, because d3dscene doesn't need to know renderqueue. In pseudo code:

mRenderQueue.UpdateVisibleIds(pD3dscene->GetIds())

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement