Forward declaration and a singleton class

Started by
4 comments, last by MARS_999 12 years ago
I am having an issue with my Manager class is a singleton and at the bottom of my header and all the classes I can add to it are above it. Problem is I would like to access the Manager class from some of the classes above it in the header and I can't figure out how to forward declare it?


class GetInput : public Event
{
public:
explicit GetInput(const std::string& id) : Event(id){}
virtual ~GetInput(){}
virtual void Run(void)
{
std::cout << "Enter input:";
sf::sleep(sf::seconds(10.0f));
std::string s;
std::cin >> s;
if(s == "quit")
NX::EventManager::Get()->SetLoop(false);
}
};
class Manager// defined here...
Advertisement
Just move the function definitions that use the singleton from the class body to after the singleton is defined.

class Manager; // forward declaring here...
class GetInput : public Event
{
public:
explicit GetInput(const std::string& id) : Event(id){}
virtual ~GetInput(){}
virtual void Run(void)
{
std::cout << "Enter input:";
sf::sleep(sf::seconds(10.0f));
std::string s;
std::cin >> s;
if(s == "quit")
NX::EventManager::Get()->SetLoop(false);
}
};
class Manager// defined here...
{

}


Note the first line, you need to forward declaring there.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.


Note the first line, you need to forward declaring there.


That's useless. A forward declaration simply says "a class of that name exists somewhere". It will NOT actually let you use that class, because the class is still undefined and it is completely unknown what is IN that class.

A forward declaration let's you do pretty much nothing but pass around pointers/references to that class. You still can't declare a variable of that type (because the size is unknown) and you can't call any functions of it (because they don't exist without actually DEFINING the class and what it looks like).

The simple matter of fact is that you can't "use" a forward declared class.

Fixing dependencies like that is usually based on NOT dumping your implementation in the header file, though in this case as SiCrane already said: DEFINE the class, before you try to use it.
f@dzhttp://festini.device-zero.de

[quote name='wqking' timestamp='1333243884' post='4927069']
Note the first line, you need to forward declaring there.


That's useless. A forward declaration simply says "a class of that name exists somewhere". It will NOT actually let you use that class, because the class is still undefined and it is completely unknown what is IN that class.

A forward declaration let's you do pretty much nothing but pass around pointers/references to that class. You still can't declare a variable of that type (because the size is unknown) and you can't call any functions of it (because they don't exist without actually DEFINING the class and what it looks like).

The simple matter of fact is that you can't "use" a forward declared class.

Fixing dependencies like that is usually based on NOT dumping your implementation in the header file, though in this case as SiCrane already said: DEFINE the class, before you try to use it.
[/quote]
What you said is that I replied in another forward declaring thread days ago. :-)
Yes, the OP need to move the function body to source file, or after the Manager class in the header, if NX::EventManager is the singleton manager (is it?).
My that kind of forward declaring only allows the "above" classes declare fields or function parameters as pointer/reference to Manager, but can't use the pointers/references until the Manager is defined.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Thanks guys, what Trienco said is what I tried and that solved it. I guess I get so automatically inlined though bound due to dumping everything into the header I forgot about this.... :(

This topic is closed to new replies.

Advertisement