[gameprogrammer] Re: Singleton (was: calling class member from another class)
- From: Justin Coleman <jmcoleman@xxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Mon, 18 Oct 2004 09:31:17 -0400
On Sun, 17 Oct 2004 20:30:49 +0200, Sebastian Beschke <s.beschke@xxxxxx> wrote:
> Justin Coleman wrote:
> > On Sun, 17 Oct 2004 10:23:01 -0300, Kevin Fields
> > <drunkendruid@xxxxxxxxxxx> wrote:
> > >Correct me if I'm wrong, but, would you need to put a pointer to it
> > >anywhere
> > >if it's a singleton?
> > >Doesn't a singleton operate in global space, allowing you to call its
> > >functions from anywhere?
> > That's what I thought too, but MSVC .net wouldn't let me do it.
> > Singleton or not, I had to have an instance of it already constructed
> > in order to call a function. The compiler wouldn't let me put a
> > function call to it inside another class without having the pointer,
> > for some reason.
>
> Generally, a singleton class needs to have an instance() method which
> returns a pointer to the (only) instance. In C++, this would have to be
> a static member function, something like this:
I apologize for my poor wording, let me try again :)
If I have two classes, say Foo and Bar I would have them in separate
source files and declared as such (just examples):
class Foo { // in foo.h and foo.cpp
public:
int foo_func();
}
#include "foo.h"
class Bar { // bar.h and bar.cpp
public:
int bar_func();
}
If I wanted to call one from the other like so:
int bar_func() // in bar.cpp
{
return foo_func();
}
...it doesn't work. Nor does "return Foo.foofunc();", or (assuming
FooInstance is declared in or before main as an instance of Foo)
"return FooInstance.foofunc();".
I have to change the definition of Bar:
class Bar {
public:
int bar_func();
class Foo* FooPtr; // this can (or should) be private if you like
}
then I can use that pointer to call Foo's functions like this:
int bar_func()
{
return FooPtr->foo_func();
}
and then assign the pointer (inside main, in this case):
Foo FooInstance;
Bar BarInstance;
BarInstance.FooPtr = &FooInstance;
// *must* assign this pointer before calling bar_func
Regardless of whether Foo is a singleton or not, the compiler won't
let me use foo_func from within Bar unless I have a FooPtr and use
that to reference the function. I'm not proficient enough yet with C++
to know if that's a language feature or just a compiler quirk (I
suspect it's language), pointers are probably my weakest point so far.
It just seems like a lot of hassle to me, so I'm combining the two
classes now, making a generic resource manager to handle both textures
and objects (and eventually sounds, etc). No messy pointers that way,
but the class files do get a bit longer. It's worth it (for my
application), IMO.
-Justin
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: Singleton
- From: Sebastian Beschke
- References:
- [gameprogrammer] Re: calling class member from another class
- From: Kevin Fields
- [gameprogrammer] Re: calling class member from another class
- From: Justin Coleman
- [gameprogrammer] Re: Singleton (was: calling class member from another class)
- From: Sebastian Beschke
Other related posts:
- » [gameprogrammer] Re: Singleton (was: calling class member from another class)
- » [gameprogrammer] Re: Singleton (was: calling class member from another class)
- [gameprogrammer] Re: Singleton
- From: Sebastian Beschke
- [gameprogrammer] Re: calling class member from another class
- From: Kevin Fields
- [gameprogrammer] Re: calling class member from another class
- From: Justin Coleman
- [gameprogrammer] Re: Singleton (was: calling class member from another class)
- From: Sebastian Beschke