Topic: ??? I'll bet this CAN'T be done! ???
Author: lapaso@enuxsa.eas.asu.edu (Anthony F. LaPaso)
Date: Sat, 12 Mar 1994 04:44:09 GMT Raw View
Hello all!
I'm fairly new to C++ and haven't come upon an answer to this
question yet. I was wondering if there was a way to declare
a static variable local to a member function such that each
instance of the class has its own copy. In other words, I want
the variable to be visible *only* within the member function
yet be able to maintain unique values for *each instance* of the
class rather than one value for the class as a whole.
The example below illustrates my point. The output of the program
is 12. Is there a way to declare x such that the output will be 11?
The obvious "solution" is to simply make x a private data member--
BUT, I want x to be visible only within func (). Is there anyway to
declare such a variable and yet get the desired result?? Thanks to
all--this questions been nagging me for quit a while.
#include <iostream.h>
class foo
{
public:
void func ();
};
void foo::func ()
{
static int x = 1;
cout << x;
++x;
}
void main ()
{
foo a, b;
a.func ();
b.func ();
}
--
Tony LaPaso Arizona State University, Tempe, Arizona
lapaso@asu.edu
Author: dak@tabaqui.informatik.rwth-aachen.de (David Kastrup)
Date: 13 Mar 1994 11:38:17 GMT Raw View
lapaso@enuxsa.eas.asu.edu (Anthony F. LaPaso) writes:
>Hello all!
>I'm fairly new to C++ and haven't come upon an answer to this
>question yet. I was wondering if there was a way to declare
>a static variable local to a member function such that each
>instance of the class has its own copy. In other words, I want
>the variable to be visible *only* within the member function
>yet be able to maintain unique values for *each instance* of the
>class rather than one value for the class as a whole.
>The example below illustrates my point. The output of the program
>is 12. Is there a way to declare x such that the output will be 11?
>The obvious "solution" is to simply make x a private data member--
>BUT, I want x to be visible only within func (). Is there anyway to
>declare such a variable and yet get the desired result?? Thanks to
>all--this questions been nagging me for quit a while.
The answer is no, and definitely no. Think about it. If every instance
of the class was to have its own static variable for this member
function, how were you supposed to do this? You can declare automatic
class variables, which all were supposed to have their own static
variables. How so? And what would be static with them?
Supposing that you declared a variable in a function, and called that
function recursively, would the different variables (automatic)
deriving from the same declaration
have the same static local variable? Hard to answer. If they did, what
use would these questionable behaviour be?
As you noted, the behaviour you probably want can be achieved by just
using a private data member (every instance of the class carries
a variable for that purpose). Now we all know that a class declaration
in C++ has to carry *all* of the information the compiler needs to
allocate the class.
If your member function was declared in another module, how should the compiler
reserve the space for it in the class?
So member functions cannot hide their needs for variables from the class.
This is a restriction of data hiding inherent to C++.
--
David Kastrup dak@pool.informatik.rwth-aachen.de
Tel: +49-241-72419 Fax: +49-241-79502
Goethestr. 20, D-52064 Aachen
Author: nagle@netcom.com (John Nagle)
Date: Sun, 13 Mar 1994 18:04:56 GMT Raw View
dak@tabaqui.informatik.rwth-aachen.de (David Kastrup) writes:
>lapaso@enuxsa.eas.asu.edu (Anthony F. LaPaso) writes:
>>Hello all!
>>I'm fairly new to C++ and haven't come upon an answer to this
>>question yet. I was wondering if there was a way to declare
>>a static variable local to a member function such that each
>>instance of the class has its own copy. In other words, I want
>>the variable to be visible *only* within the member function
>>yet be able to maintain unique values for *each instance* of the
>>class rather than one value for the class as a whole.
>The answer is no, and definitely no. Think about it. If every instance
>of the class was to have its own static variable for this member
>function, how were you supposed to do this? You can declare automatic
>class variables, which all were supposed to have their own static
>variables. How so? And what would be static with them?
Not quite. You can nest class declarations, although it is a bit
unwieldy.
John Nagle
Author: peters@phibm60c.Physik.Uni-Augsburg.DE (Peter Schmitteckert (Tel. 5977-246))
Date: Sun, 13 Mar 1994 19:11:07 GMT Raw View
In article <CMJBto.FFx@ennews.eas.asu.edu>, lapaso@enuxsa.eas.asu.edu (Anthony F. LaPaso) writes:
... [deleted]
|> question yet. I was wondering if there was a way to declare
|> a static variable local to a member function such that each
|> instance of the class has its own copy. In other words, I want
|> the variable to be visible *only* within the member function
|> yet be able to maintain unique values for *each instance* of the
|> class rather than one value for the class as a whole.
|>
... [deleted]
|> Tony LaPaso Arizona State University, Tempe, Arizona
|> lapaso@asu.edu
|>
Hallo Anthony F. LaPaso,
I hope the following will solve your problem:
//*******************
#include <iostream.h>
class stat_foo
{ private: int x;
public : stat_foo(void) { x=1;};
void func ();
};
class foo: public stat_foo
{ public : foo():stat_foo() { };
// void err_foo() { x=42; cout<<x;};
};
void stat_foo::func() { cout << x; ++x; }
void main ()
{
foo a, b;
a.func ();
b.func ();
}
//*******************
The output is indeed '11'.
Bye Peter
------------------------------------------------------------
Peter Schmitteckert
Lehrstuhl fuer Theoret. Physik 2
Institut fuer Physik
Uni Augsburg (Germany)
e-mail: Schmitteckert@Uni-Augsburg.de
Phone: +49-821-5977-246
Fax: +49-821-5977-262
Author: peot@elements.rpal.rockwell.com (Mark Alan Peot)
Date: 13 Mar 94 19:14:02 Raw View
>>Hello all!
>
>>I'm fairly new to C++ and haven't come upon an answer to this
>>question yet. I was wondering if there was a way to declare
>>a static variable local to a member function such that each
>>instance of the class has its own copy. In other words, I want
>>the variable to be visible *only* within the member function
>>yet be able to maintain unique values for *each instance* of the
>>class rather than one value for the class as a whole.
>
>>The example below illustrates my point. The output of the program
>>is 12. Is there a way to declare x such that the output will be 11?
>>The obvious "solution" is to simply make x a private data member--
>>BUT, I want x to be visible only within func (). Is there anyway to
>>declare such a variable and yet get the desired result?? Thanks to
>>all--this questions been nagging me for quit a while.
>
>The answer is no, and definitely no. Think about it. If every instance
>of the class was to have its own static variable for this member
>function, how were you supposed to do this? You can declare automatic
>class variables, which all were supposed to have their own static
>variables. How so? And what would be static with them?
Sorry, but you are wrong.
Say that your class is called OUTSIDE. Define a slot (call it "inside") that
is of class type INSIDE. INSIDE protects the data that you find so special.
INSIDE overloads operator() to provide the member function that you desire.
pseudo code (of course I haven't tested it.):
class OUTSIDE {
class INSIDE {
private:
int protected_data;
public:
int operator(args) {...code...};
} inside;
};
As to whether this is a useful technique or not: I really believe that there
is such a thing as overdesigning your code. Use a private member slot for your
OUTSIDE class that holds your data. In your class, you control how the data
is used by a revolutionary mechanism: a comment.
Mark Alan Peot
Information and Decision Sciences Phone: (415) 325-7143
Rockwell International Science Center Fax: (415) 325-2007
Palo Alto, California 94301 Page: (415) 496-0900
Author: jjb@watson.ibm.com (John Barton)
Date: Mon, 14 Mar 1994 13:12:36 GMT Raw View
Put func() in a base class and make int x a private member.
Then erase all brain cells connected to "static variable local".
It will save you some grief.
John.
In article <CMJBto.FFx@ennews.eas.asu.edu>, lapaso@enuxsa.eas.asu.edu (Anthony F. LaPaso) writes:
|> Hello all!
|>
|> I'm fairly new to C++ and haven't come upon an answer to this
|> question yet. I was wondering if there was a way to declare
|> a static variable local to a member function such that each
|> instance of the class has its own copy. In other words, I want
|> the variable to be visible *only* within the member function
|> yet be able to maintain unique values for *each instance* of the
|> class rather than one value for the class as a whole.
|>
|> The example below illustrates my point. The output of the program
|> is 12. Is there a way to declare x such that the output will be 11?
|> The obvious "solution" is to simply make x a private data member--
|> BUT, I want x to be visible only within func (). Is there anyway to
|> declare such a variable and yet get the desired result?? Thanks to
|> all--this questions been nagging me for quit a while.
|>
|> #include <iostream.h>
|> class foo
|> {
|> public:
|> void func ();
|> };
|>
|> void foo::func ()
|> {
|> static int x = 1;
|> cout << x;
|> ++x;
|> }
|>
|> void main ()
|> {
|> foo a, b;
|> a.func ();
|> b.func ();
|> }
|> --
|> Tony LaPaso Arizona State University, Tempe, Arizona
|> lapaso@asu.edu
|>
--
John.
John J. Barton jjb@watson.ibm.com (914)784-6645
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598