|
programming help
|
View this Thread in Original format
| kofrad |
| im working on this simple game engine in my spare time. ive come to a problem though. i need to be able to use a class without initializing it. i dont really know what this is called, but i am positive ive done it in php. i need to be able to either use a class without initializing it or have the class in a global scope, available from multiple C++ source files. if anyone has any idea what im talking about please do try to help, im positive the feature is there, i just dont know what its called, and therefore cannot find any information on it |
|
|
| LeopoldStotch |
| my C++ is very vague at the moment, because I have been using Java a lot these past couple of months. But they are 75% similar in a way. I think in C++ there is a way to declare a method "static" or a class "abstract" ?? My inheritance declerations in C++ is vague. |
|
|
| kofrad |
| quote: | Originally posted by LeopoldStotch
my C++ is very vague at the moment, because I have been using Java a lot these past couple of months. But they are 75% similar in a way. I think in C++ there is a way to declare a method "static" or a class "abstract" ?? My inheritance declerations in C++ is vague. |
that may be it, ill have to re-read some stuff on inheritance. if its more help, i want something like this
| quote: |
#include "cGame.h"
...
cGame::Init();
cGame::Run();
...
|
instead of something like this
| quote: |
#include "cGame.h"
...
cGame game;
game.Init();
game.Run();
...
|
the first example requires no initialization and from my knowledge keeps the class available to any file that includes the class header into it. |
|
|
| kofrad |
i think ive found what i need to do. im going to need to implement a singleton into my engine.
now i gotta figure out how to implement this into my code, then i gotta finish coding the graphics class and code the sprite manager class :nervous: |
|
|
| TazZ-erT |
| Jus reinstall windows |
|
|
| loconet |
| quote: | Originally posted by kofrad
i think ive found what i need to do. im going to need to implement a singleton into my engine.
now i gotta figure out how to implement this into my code, then i gotta finish coding the graphics class and code the sprite manager class :nervous: |
No need for Singletons. Singletons, the design pattern, happen to use the concept you are looking for but they are not your answer.
What you want to do is create a static member function (PHP 5 has this)..
in c++:
code:
class cGame {
/*any other member functions/variables..*/
public:
static void init() { /*do whatever.*/ }
};
More.. http://cplus.about.com/od/beginnerc...l/aa080802d.htm |
|
|
| kofrad |
| quote: | Originally posted by loconet
No need for Singletons. Singletons, the design pattern, happen to use the concept you are looking for but they are not your answer.
What you want to do is create a static member function (PHP 5 has this)..
in c++:
code:
class cGame {
/*any other member functions/variables..*/
public:
static void init() { /*do whatever.*/ }
};
More.. http://cplus.about.com/od/beginnerc...l/aa080802d.htm |
i havent added any new code for singletons, but i did have success in making the instances of a class static, then including the file with all instances in each file that would need it
code:
#include "cStateManager.h"
#include "cProcessManager.h"
...
static cStateManager cGameStateManager;
static cProcessManager cGameProcessManager;
would this be enough, or should i declare certain methods and members as static within the class declaration? |
|
|
| Demoted |
file > go to > hack the mainframe
pretty much that. |
|
|
| loconet |
| quote: | Originally posted by kofrad
i havent added any new code for singletons, but i did have success in making the instances of a class static, then including the file with all instances in each file that would need it
code:
#include "cStateManager.h"
#include "cProcessManager.h"
...
static cStateManager cGameStateManager;
static cProcessManager cGameProcessManager;
would this be enough, or should i declare certain methods and members as static within the class declaration? |
You still need to declare the individual functions as static if you wish to do something like cGameStateManager::init();. What you did is only declaring the class instances as static. |
|
|
| kofrad |
| quote: | Originally posted by loconet
You still need to declare the individual functions as static if you wish to do something like cGameStateManager::init();. What you did is only declaring the class instances as static. |
im able to access methods like cGameGraphics::Initialize() with only the instance declared as static, but if i make the methods static, i get an error:
code: /home/kofrad/sdl_game_engine/src/cGraphics.cpp:32: error: cannot declare member function `static bool cGraphics::Initialize(int, int, int, bool, int)' to have static linkage
|
|
|
| AnotherWay83 |
| quote: | Originally posted by TazZ-erT
Jus reinstall windows |
:haha: :crazy: |
|
|
|
|