Re: Creating member vars on the heap, not the stack
> Singleton& getInstance() {
> static Singleton s;
> return s;
> }
>
> A static variable declared in a function is initialized when the
> function is first called.
Expanding the class to include a buffer
unsigned int m_Buff[10000];
Is the instance of 'Singleton ' created on the stack here? If so, it
makes no sense to create the member data 'm_Buff' on the stack because
that data is never destoryed, the singleton exists for the duration of
the application, taking up stack space, it should be in the heap.
So then i need to use new to create the data, and store a uint* in the
class?
Is this the correct way of going about things?
Igor Tandetnik wrote:
"JoeB" <joe@nospam.com> wrote in message
news:uBetsluEHHA.1748@TK2MSFTNGP02.phx.gbl
I have a class that is a singleton. There will only ever be one
instance, and it is in scope for the entire duration of the program.
for this reason, i would like the member vars in the class to be
stored on the heap, not the stack.
Other than declaring all the members of the class as static, is there
another way?
When you say "on the heap", you actually mean in static memory. A heap
is where dynamically allocated objects go, as in
C* pC = new C(); // an instance of class C is now on the heap
The class its self cannot be declared as static, because that causes
the c'tor to be called before main(), which is a definite no-no.
Singleton& getInstance() {
static Singleton s;
return s;
}
A static variable declared in a function is initialized when the
function is first called.