473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to change instance variable value in instance function

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!
Aug 3 '08 #1
4 1924
Ed wrote:
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.

--
J.
Aug 3 '08 #2
Jeroen Mostert wrote:
Ed wrote:
>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.
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.

--
David Wilkinson
Visual C++ MVP
Aug 3 '08 #3
Ed
On Aug 3, 9:58 pm, David Wilkinson <no-re...@effisols.comwrote:
Jeroen Mostert wrote:
Ed wrote:
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.

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.

--
David Wilkinson
Visual C++ MVP
Thank you!
I just don't want the static variable to be declared as a class
member.

Ed.
Aug 4 '08 #4
On Aug 4, 6:02*am, Ed <seah...@gmail.comwrote:
>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?

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();
}
};
Aug 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
12
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
3
by: Byron | last post by:
Hi, Javascript confuses me, so I usually limit myself to Dreamweaver's built-in scripts for stuff like imageswaps. But this time I'm trying to write something very simple myself. I do most of my...
1
by: Varadha | last post by:
Hi, I am declaring a variable static char Version_No = '1' in header file header.h In a application "app.exe", i am changing the value of the variable in to '2' In the same application i am...
7
by: dotnetnoob | last post by:
i keep getting Object references not set to an instance of an object from this code: Private Sub EqBinding() Dim x As Integer x = 0 Do If CStr(arlsType.Item(x)) = "Bacnet Point" Then Dim...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
1
SamKL
by: SamKL | last post by:
Hey, I'm no expert on PHP, and I have somewhat of an understanding of object oriented code. Anyway, getting right to the problem. I'm using PHP4, so base it off of that. Basically I have 2...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.