• 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
Meerul264

Forward declaration, how and when do we use it?

8 posts in this topic

Now I know they're already a lot of threads talking about this. But no matter how much I read, I still can't seem to understand! So I decided to give it a try here - plus, I can ask a question myself here.

 

Suppose I have 2 classes; class A and class B. Both Have their own .cpp and .h files. I heard that if two classes need to use each other's member variables (quite often in game programming, I've come across a lot, but don't know how to.)

 

So how do I do forward declaration so that class A can access class B, and class B can access class A?

I might want to implement this on inventory - item class for my game.

 

Note that class A and class B are NOT declared and defined in the same .cpp file. Thanks :)

 

0

Share this post


Link to post
Share on other sites

A.h:

class B; //forward declaration

class A
{
//contents
}

B.h:

class A;

class B
{
//contents
}

A.cpp:

#include "A.h"
#include "B.h"

//code 

B.cpp:

#include "B.h"
#include "A.h"

//code

This is the safest way.  Basically you can't include a file that includes the first file.  

 

Forward declaring a class allows you to define pointers using that type, but not access them.  

 

So in class A you can have

 

B* b;

 

but you can't have an inline constructor that calls a function in b or accesses its variables.

 

Since you don't usually #include a .cpp file directly, it's safe to just include all the classes .h files in your .cpp files

Edited by zacaj
0

Share this post


Link to post
Share on other sites


Forward declaring a class allows you to define pointers using that type, but not access them.  

 

I'm don't follow this. Example please? thanks :)

0

Share this post


Link to post
Share on other sites
class Foo;

Foo *foo_pointer; //valid.  It's just a pointer, so it only needs to know that Foo is a type (which it does from the forward declaration), it doesn't need to know anything about Foo itself

Foo foo;  //error.  Actually making a variable of type Foo (foo_pointer is of type Foo*, not Foo) can't be done, because we don't know how big Foo is.

void accessFoo(Foo *foo)
{
   foo->x=4;//also an error.  We don't know what variables Foo contains, so there's no way we could access them
}

class Foo
{ 
public:
    int x;
};
//declaring the type Foo down here doesn't affect the code above it

Foo foo2; //this is now valid, because Foo is a fully defined type

void main()
{
    foo_pointer=new Foo();//although foo_pointer was declared above the Foo class, since *this* code is below the Foo class, it can access foo_pointer's members
    foo_pointer->x=8;
    accessFoo(foo_pointer);
}

0

Share this post


Link to post
Share on other sites

typically for an inventory system, you'll have a master database of inventory item types, and some sort of list data structure that can hold a list of instances of individual items.

 

i call mine a "stufflist". <g>      

 

its a list of stuff.  

 

stuff laying on the ground in a given area, stuff the player is carrying, stuff a NPC has for use in combat, stuff you find in a treasure chest, stuff available for trade, stuff installed on a vehicle, stuff carried in a cargo bay, etc.  

 

then different things in the game (PCs, npcs, stores, treasure chests, vehicles, etc) have their own stufflists.

 

all inventory actions are about adding items to, removing items from, or transferring items between, stufflists.

 

this usually does not require circular dependency between the parts of the system.

 

the item types database has the data such as base price, weight, is_weapon, weapon_type, is_missile_weapon, ammo_type, damage, rate of fire, time to manufacture, rendering info (how to draw it), etc. 

 

the stufflists have the ability to add and remove objects, and perform calculations such as total weight of items in the list, and can do searches for items based on type, quality, etc.

 

so at the low level you have the item types database, at the mid level you have the stufflist API for managing items, and at the high level you have the controlling code that manipulates the stuff lists to do things such as move all the items in the treasure chest to the player's inventory ("Take all").

 

note that each stufflist is in essence a little database of its own. fields in its records would be things like itemtype, quantity, quality, % completed (if under construction / being manufactured), position and rotation (for dropped items), etc.

 

things like encumbrance checks, container checks (do they have enough containers to carry it?), out of cargo space checks, out of hull space checks, etc would be done by the controlling code.

0

Share this post


Link to post
Share on other sites

Meerul264,

You may have been misinformed about forward declarations.  One thing a forward declaration does not give is visibility on class members. 

 

Gaining Visibility on Class Definitions

 

This is generally done by including headers.  As zacaj shows above, A.cpp can include B.h to see and use B's public members, and B.cpp can include A.h to see and use A's public members. 

 

A.h:

class A
{/*members declared here*/};

B.h:

class B
{/*members declared here*/};

A.cpp:

#include "A.h" // Includes A's class definition (so members declared in it can be defined here).
#include "B.h" // Includes B's class definition (so members of A can see and use B and its public members).
...

B.cpp:

#include "B.h" // Includes B's class definition (so members declared in it can be defined here).
#include "A.h" // Includes A's class definition (so members of B can see and use A and its public members).
...

Header inclusion gets you visibility on a class definition.  Forward declarations are neither necessary nor sufficient.

 

When to Use Forward Declarations

 

Forward declarations are for cases in which the compiler needs to know that something is defined elsewhere, but doesn't need the details of that definition. 

 

A.h:

class B; // Forward declaration

class A
{B * pB;};  // Use of B, but not of B's definition

In this case, the compiler doesn't need to know anything about B here except that it's a class which is defined somewhere.

 

Note that we could include B.h here instead.  However, that would unnecessarily include B.h everywhere that A.h is included (which is bad for compile times). 

 

Also, there are cases when you can't include a header:

 

A.h:

#include "B.h" // Error - B.h includes this file

class A
{B * pB;}; 

B.h:

#include "A.h" // Error - A.h includes this file

class B
{A * pA};

Trying to include header files here results in infinite include recusion.

 

Include guards and "#pragma Once" can't solve this issue.  They will prevent the recursion, but will result in a reference to an undefined type.  If you can imagine the compiler reading A.h, it would include B.h, whithin which it would effectively ignore the directive to include A.h, then would parse the definition of B, which includes a reference to A, which would not yet be defined.

 

Forward declaration is the only way to make this work.

 

That being said, you'll run into other problems when you have classes mutually depending on each other in this way.  As mentioned above, in this case you should probably re-design to reduce dependancies.  I generally only use forward declarations to eliminate unnecessary include directives.

 

Forward Declarations Can Only Do So Much

 

Note that forward declarations allow A and B to contain pointers to each other because the compiler doesn't need to know the details of a class to define a pointer to that class.  This wouldn't work for member objects.

 

A.h:

#include "B.h" 

class A
{B MyB;}; 

In this case we must include the full definition of B.  The compiler needs its details in order to fully define A.  Because of this, there's no way for B to then contain a member of type A.  Forward declarations don't make recursive class definitions possible.

Edited by VReality
0

Share this post


Link to post
Share on other sites

Generaly, forward declaration is neccesary only if the declaration of class A needs to understand declarations of class B and vice versa, so it does not apply for definitions. To put siimply, suppose you have class CNode and class CScene. CScene needs to know about CNode, and suppose CNode definition needs to know about CScene as well, BUT, only on the definition manner. See this.

 

CNode.cpp:

#include "CScene.h" // vital and unavoidable!

CNode::Release()

{

 m_pScene->RemoveNode(this); // m_pScene is of CScene* type;

}

 

If you want to write such a definition of a CNode function, you need to include CScene.h header to CNode.cpp, ||||BUT THIS IS POSSIBLE ONLY IF CScene,h does not include Cnode.h!||||. But unluckily you find yourself in CScene declaration in following situation:

 

CScene.h:

#include "Cnode.h" // can be removed if following line is uncommented

// class CNode; // this is forward declaration, needed to type members of somewhere in declaration of CScene

class CScene

{

.......

CNode* GetNodeByName(char* name);

......

}

Do you see that forward declaration allowed leaving include of CNode.h from CScene.h , thus allowing to use CScene.h in CNode.cpp  definitions while having CScene members correctly typed if they use CNode on declaratoin level.

0

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