How to change instance variable value in instance function

E

Ed

Hi, guys,
I am wondering if I could change the static variable value in one
static function?

If I have a class:

class Apple
{
public:
static Apple* Instance()
{
static Apple* pInstance(NULL);
if( ! pInstance)
{
pInstance = new Apple();
}
return pInstance;
}
}


Can I change the pInstance from outside?
Something like: Apple::Instance()::pInstance = NULL;

Thanks!
 
J

Jeroen Mostert

Ed said:
If I have a class:

class Apple
{
public:
static Apple* Instance()
{
static Apple* pInstance(NULL);
if( ! pInstance)
{
pInstance = new Apple();
}
return pInstance;
}
}


Can I change the pInstance from outside?

No, unless you somehow pass a pointer or reference to the variable to the
outside world from the function. But that's such a mind-bogglingly silly
violation of encapsulation that it's not worth considering.

If you want this ability, then don't use function-scoped static variables.
Use a static class member instead. If you can't modify the original class,
then consider the possibility that there's a very good reason why you're not
supposed to change this value from the outside.
 
D

David Wilkinson

Jeroen said:
No, unless you somehow pass a pointer or reference to the variable to
the outside world from the function. But that's such a mind-bogglingly
silly violation of encapsulation that it's not worth considering.

If you want this ability, then don't use function-scoped static
variables. Use a static class member instead. If you can't modify the
original class, then consider the possibility that there's a very good
reason why you're not supposed to change this value from the outside.

Ed:

I don't know why you want to do this, but one way would be

static Apple* Instance(bool bReset = false)
{
static Apple* pInstance(NULL);
if( bReset )
{
delete pInstance;
pInstance = NULL;
}
else if( ! pInstance )
{
pInstance = new Apple();
}
return pInstance;
}

BTW, since this is a question on standard C++ (not C++/CLI), you would be better
to ask it in microsoft.public.vc.language.
 
E

Ed

Ed:

I don't know why you want to do this, but one way would be

static Apple* Instance(bool bReset = false)
{
static Apple* pInstance(NULL);
if( bReset )
{
delete pInstance;
pInstance = NULL;
}
else if( ! pInstance )
{
pInstance = new Apple();
}
return pInstance;

}

BTW, since this is a question on standard C++ (not C++/CLI), you would be better
to ask it in microsoft.public.vc.language.

Thank you!
I just don't want the static variable to be declared as a class
member.

Ed.
 
P

Pavel Minaev

I just don't want the static variable to be declared as a class
member.

Why, really?

But anyway, here's another trick:

class Apple
{
static Apple*& pInstance()
{
static Apple* value(NULL);
return value;
}

static Apple* Instance()
{
if( ! pInstance())
{
pInstance() = new Apple();
}
return pInstance();
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top