473,786 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static class local variable in memeber function

By doing:

void MyClass::MyFunc tion()
{
static int myvar;
....
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.

?
Aug 12 '05 #1
5 2179
john wrote:
By doing:

void MyClass::MyFunc tion()
{
static int myvar;
...
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.

?


Make it a member variable.

Aug 12 '05 #2

john schreef:
By doing:

void MyClass::MyFunc tion()
{
static int myvar;
...
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.


std::map<MyClas s*, int> myvars;
myvars[this] = ...

HTH,
Michiel Salters

Aug 12 '05 #3
* john:
By doing:

void MyClass::MyFunc tion()
{
static int myvar;
...
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.

?


Not really.

But consider that if 'myvar' is to be restricted to a _single_ function,
then it is a local variable.

And if it is to be restricted to a _set_ of member functions, then the
functionality it partakes in is also restricted to that set, so the set
forms its own class. I.e. you can make that set of member functions, plus
variable, a separate class that 'MyClass' inherits from. Or that 'MyClass'
contains an instance variable of.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 12 '05 #4
Rolf Magnus a écrit :
john wrote:

By doing:

void MyClass::MyFunc tion()
{
static int myvar;
...
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.

?

Make it a member variable.

Well and it can be accedeed from outside the function. you did not
understand the point.
Aug 12 '05 #5
john wrote:
Rolf Magnus a écrit :
john wrote:

By doing:

void MyClass::MyFunc tion()
{
static int myvar;
...
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.

?
Make it a member variable.

Well and it can be accedeed from outside the function.


So what? Make it private, so only members of this class can use it.
you did not
understand the point.


What problem are you trying to solve and why would a private non-static
member variable not work?

V
Aug 12 '05 #6

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

Similar topics

1
2035
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to work. The error states: Object.h(16): error C2662: 'DataLib::Object::_type' : cannot convert 'this' pointer from 'const DataLib::Object' to 'DataLib::Object &' namespace DataLib
16
2988
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
9
6367
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
3
3937
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /* in above case where is variable k and j mapped in memory layout ? */
28
4647
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can anyone confirm this. Thanks. -- Dennis in Houston
55
6249
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
14
6027
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
16
8627
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions - which is Very Bad (tm) for reasons I understand! However, does this rule apply only to global static variables, or does it apply to procedure-level static variables.
3
1796
by: Bryan Parkoff | last post by:
The local variables and local functions are inside class body. You define a variable to the class "Reg reg;" in the main function. The reg variable has a pointer. The pointer gives memory address. The memory address accesses local variable and local function. It is fine design according to your preferance. I try to remove a pointer so I let local variable and local function as global to access to the memory directly without needing a...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.