473,466 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

class variables: scoping

Hello,

Suppose I have some method:

Foo::foo() {
static int x;
int y;
/* ... */
}

Here variables x is shared by all instances of the class, but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.

Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
Essentially, I would like to have something like the equivalent of:

class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};

However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().

The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.

It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:

Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}

Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().

I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition. It's like having to make
things global just because they are shared by instances.
Comments welcome.

Regards,

Neil
Jul 22 '05 #1
1 952
On 3 Apr 2004 13:23:11 -0800, nz******@cs.mun.ca (Neil Zanella) wrote:
Hello,

Suppose I have some method: How about calling them "functions" now? You've been around for a while...
;-)

Foo::foo() {
static int x;
int y;
/* ... */
}

Here variables x is shared by all instances of the class,
Not /that/ variable x, if you're referring to the one in Foo:foo(). That's
a block-scope static, which means it can only be referred to within that
function (and its value persists across multiple calls to it. By the way,
it gets initialized to zero the first time the function is called, if you
don't give it an initializer.)

but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.
Not by the code you've shown. Automatic locals without an initializer are
left uninitialized.

Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
So far you've described a simple local automatic variable.
Essentially, I would like to have something like the equivalent of:

class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};
Oh boy.

However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().
The x you actually defined up there in foo() satisfies this (but not what
you go on to specify below.)

The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.
I /think/ I see what you want, but there's no primitive way to declare that
in C++: You want an instance variable (one copy associated with each
instance of an object) that is only in scope within a particular function,
right? No can do. You'll just have to exercise some self-discipline by
declaring a private data member and being careful to only access it from
within the one function.

It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:

Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}

Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().
Lots of things would be nice. This one isn't going to happen, though.
I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition.
To indicate to the compiler that it has to make room for it in each object
instantiated. At least the way C++ is currently defined ;-)
It's like having to make
things global just because they are shared by instances.
Comments welcome.
You got 'em.
-leor

Regards,

Neil


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

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

Similar topics

30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
29
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
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
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.