Sign in to follow this  
KnightAdz

c++ help with starting new project

Recommended Posts

KnightAdz    100
Hi, I'm trying to start a new project and have already run into a couple of issues, all quite minor but I would like some help/advice please. Problem 1: My header file has 2 classes that both have references to each other in them, so it can't compile, is there anyway round this? i.e. Class Screen { public: //pointer to screen manager ScreenManager* pScreenManager; } Class ScreenManager { private: std::vector<Screen> listOfScreens; } Problem 2: In C# I have an enum of ScreenStates that can accessed everywhere (I think because it is in a namespace), is it a good idea to create a namespace for most of my game, or is that bad programming practise? If it is bad practise, what is the best way to create an enum (e.g. enum ScreenState{ Active,Inactive, Paused,};) so that the screenmanager and other areas of the game can access it? I assume I could achieve this by making it a global variable, however I just wanted to see if this was necessary or not... Thanks for any responses Adam

Share this post


Link to post
Share on other sites
zyrolasting    280
Hey KnightAdz!

Quote:
Problem 1:
My header file has 2 classes that both have references to each other in them, so it can't compile, is there anyway round this? i.e.
Class Screen
{
public:
//pointer to screen manager
ScreenManager* pScreenManager;
}
Class ScreenManager
{
private:
std::vector<Screen> listOfScreens;
}


You declare ScreenManager after you make use of the class' name. Since you are using a pointer to the object, this is fine as long as you make a forward declaration.

class ScreenManager; // <-- Forward declaration. Make a habit of using these.

class Screen
{
public:
//pointer to screen manager
ScreenManager* pScreenManager;
}
class ScreenManager
{
private:
std::vector<Screen> listOfScreens;
}


However, if you were trying to keep a reference or copy of a ScreenManager before it's actual layout is visible, you need to move the layout before the code that uses it.

Quote:
In C# I have an enum of ScreenStates that can accessed everywhere (I think because it is in a namespace), is it a good idea to create a namespace for most of my game, or is that bad programming practise? If it is bad practise, what is the best way to create an enum (e.g. enum ScreenState{ Active,Inactive, Paused,};) so that the screenmanager and other areas of the game can access it? I assume I could achieve this by making it a global variable, however I just wanted to see if this was necessary or not...


There is not much point to tossing an engine in a namespace unless you need to resolve ambiguity. If you had a bunch of symbols other libraries might use like 'list', or 'map' you would want to isolate the symbols in a namespace to avoid name collisions. The STL is full of objects with names other people might use ('list' and 'map' included) and the developers were courteous enough to throw the entire thing in namespace std.

However, in a game engine you can get more specific. For example, when I write a game I would name the class with the initialization logic and the message loop after the game, followed by App.

class starcraftApp; // With apologies to Blizzard.


That's just me. I don't expect to be using any other code that has an object called starcraftApp, so I don't throw it in a namespace. It saves me from typing 'using namespace' everywhere, which gets VERY annoying for me, especially when I realize that by doing so I effectively defeat the purpose of the namespace since I just keep pushing the code to global scope.

Also, declaring an enum does not make an instance of it's type magically appear. It's just another layout. You would declare the enum and then declare a variable of it's type elsewhere.

enum colors
{
RED,
GREEN,
BLUE
};

int main()
{
colors c = GREEN;

//...
return 0;
}


If you need any help in the future with C++, drop me a PM.

Hope this Helps, and Cheers!
-Zyro

Share this post


Link to post
Share on other sites
zyrolasting    280
Quote:
you might regret that offer of the PM


If you truly do not understand something I will be happy to help. [smile]
It is true I don't want to be answering every little thing, though. A great tutorial and reference for C++ can be found here.

Glad I helped!
-Zyro

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