• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

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

5 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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. Edited by frob
1

Share this post


Link to post
Share on other sites

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)

Edited by Servant of the Lord
1

Share this post


Link to post
Share on other sites

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.
-1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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())

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0