473,763 Members | 7,541 Online
Bytes | Software Development & Data Engineering Community
+ 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
30 2271
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
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 #3
Neil Zanella wrote:
[...]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;
/* ... */
}
If I understood correctly, you want to associate a variable (state) to a
member function (behavior)... I would say you need a new object for that
(and a class, of course). Your little object would have just one method,
so I guess it's safe to call it a functor, maybe overwrite the "()"
opertor to look more like a function...
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.


I'm not sure it is that common to justify inclusion in the language..
and it's not that difficult to get the same effect using existing
language constructs.

regards,
iuli
Jul 22 '05 #4
Neil Zanella wrote:
[...]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;
/* ... */
}
If I understood correctly, you want to associate a variable (state) to a
member function (behavior)... I would say you need a new object for that
(and a class, of course). Your little object would have just one method,
so I guess it's safe to call it a functor, maybe overwrite the "()"
opertor to look more like a function...
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.


I'm not sure it is that common to justify inclusion in the language..
and it's not that difficult to get the same effect using existing
language constructs.

regards,
iuli
Jul 22 '05 #5
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
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:


Basically, a private data member accessible to one or more member functions.

I tried the following once but it fails compile. But maybe we can tweak it
a bit, and then it will compile? Maybe with the pointer to implementation
concept it would work?

class Foo;

class Foo_f_Data
{
int x;
friend void Foo::f(); // this line fails compile because class Foo not
defined
};

class Foo : private Foo_f_Data
{
public:
void f();
void g();
};

void Foo::f() {
x = 3; // ok
}

void Foo::g() {
x = 5; // fails compile, c
}

Of course, comments are a nice idea too!
Jul 22 '05 #6
"Neil Zanella" <nz******@cs.mu n.ca> wrote in message
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:


Basically, a private data member accessible to one or more member functions.

I tried the following once but it fails compile. But maybe we can tweak it
a bit, and then it will compile? Maybe with the pointer to implementation
concept it would work?

class Foo;

class Foo_f_Data
{
int x;
friend void Foo::f(); // this line fails compile because class Foo not
defined
};

class Foo : private Foo_f_Data
{
public:
void f();
void g();
};

void Foo::f() {
x = 3; // ok
}

void Foo::g() {
x = 5; // fails compile, c
}

Of course, comments are a nice idea too!
Jul 22 '05 #7
* nz******@cs.mun .ca (Neil Zanella) schriebt:

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.

class Foo;

class FooFunctor
{
private:
Foo& myFoo;
int myZ;
public:
FooFunctor( Foo& aFoo ): myFoo( aFoo ), myZ( 0 ) {}
int operator()();
};

class Foo
{
private:
int x;
FooFunctor foo;
public:
Foo(): x(0), foo(*this) {}

void bar()
{
x = foo();
}
};

int FooFunctor::ope rator()(){ ... }

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().


As I recall there is a proposal to add members with automatic back-
pointers, like Java inner classes, but whether it will be adopted...

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8
* nz******@cs.mun .ca (Neil Zanella) schriebt:

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.

class Foo;

class FooFunctor
{
private:
Foo& myFoo;
int myZ;
public:
FooFunctor( Foo& aFoo ): myFoo( aFoo ), myZ( 0 ) {}
int operator()();
};

class Foo
{
private:
int x;
FooFunctor foo;
public:
Foo(): x(0), foo(*this) {}

void bar()
{
x = foo();
}
};

int FooFunctor::ope rator()(){ ... }

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().


As I recall there is a proposal to add members with automatic back-
pointers, like Java inner classes, but whether it will be adopted...

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #9
Leor Zolman <le**@bdsoft.co m> wrote in message:
Foo::foo() {
static int x;
int y;
/* ... */
}

Here variable 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.)


Perhaps my explanation was somewhat inadequate. What you have stated in the
above paragraph is correct. Indeed, x can only be referred to from within
member function Foo::foo(). And indeed since it is static the compiler will
automatically arrange for it to be initialized to zero when no initializer
is present. What I meant to express by saying that x is shared by all
instances of Foo is exemplified in the following code:

#include <iostream>

class Foo {
public:
void foo() {
static int x;
std::cout << x++ << std::endl;
}
};

int main() {
Foo foo;
foo.foo();
foo.foo();
Foo bar;
bar.foo();
Foo foobar;
foobar.foo();
}

Output:

0
1
2
3

The goal is to achieve the output:

0
1
0
0

without changing the contents of main and without changing the contents
of the class definition (which seems impossible in C++, since it does not
support the feature I describe).
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,


Correct. That is indeed the feature I was describing. Furthermore I was
poining out that C++ supports combinations:

(one copy per class, class scope) (static data members in class body)
(one copy per class, function scope) (static data members in function body)
(one copy per instance, class scope) (automatic data in class body)

but not the combination:

(one copy per instance, function scope)

I think this is poor design of the C++ language, because several functions
need have boolean variables called initialize which act as follows. Class
Foo has methods foo(), bar(), and foobar(). We don't want the constructor
to call these because they consume too much precious time, sometimes only
to initialize data members that are not needed later on. So we have the
functions foo(), bar(), and foobar() do the respective data member
initializations they need when they are called. Given that the
sets of data members they initialize are disjoint in the
given scenario, and that foo() may initialize more than
one data member, it makes sense to have a variable
which satisfies the missing C++ feature in order
to perform the initialization when foo() is called.
Alas, I need put such initializer variables inside
the function body.

I wonder if any languages have what I describe. Smalltalk?
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.

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 ;-)


Hmmm... but the compiler went and fetched those static variables for
the BSS segment. I gues they belong to another data segment altogether
hence that is what makes it possible???

Regards,

Neil
Jul 22 '05 #10

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

Similar topics

29
428
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
166
8662
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
7
2115
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 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9937
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,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
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
5270
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.