use of static variables in C++ objects 
Author Message
 use of static variables in C++ objects

My goal is to define a class where some variables are static.
When I need an object of that class, I define an new object, but I would
like to
keep the some variables contents.
Is there any technique to realize this. I was said that in C++, it is better
to avoid global variables.

Thanks for any help

Jeff



Sun, 30 Jul 2000 03:00:00 GMT  
 use of static variables in C++ objects

Quote:

> My goal is to define a class where some variables are static.
> When I need an object of that class, I define an new object, but I would
> like to keep the some variables contents.

When you define a class member variable as static it becomes, in effect, a
global variable for all instances of that class.  It will remain constant for
all new objects of that class unless you explicitly change it.

Does that help?

Jim [VC/MFC MVP]
To send mail, change spam-me-not to msn



Sun, 30 Jul 2000 03:00:00 GMT  
 use of static variables in C++ objects

You can place static variables inside member functions so that they can only
be accessed by that function, and you can assign a value to it in the
function.  Like this:

void MyClass::Foo()
{
  static int counter = 0;
   .
   .
   counter++;

Quote:
}

This value will be assigned to it only the first time the function is
called, and it will keep it's value between function calls.  And if I'm not
mistaken, there will be only one value shared across all instances of this
class (although I might be wrong about that).

Steven Schulze
Concord,  CA

Quote:

>My goal is to define a class where some variables are static.
>When I need an object of that class, I define an new object, but I would
>like to
>keep the some variables contents.
>Is there any technique to realize this. I was said that in C++, it is
better
>to avoid global variables.

>Thanks for any help

>Jeff



Sun, 30 Jul 2000 03:00:00 GMT  
 use of static variables in C++ objects

Quote:

>void MyClass::Foo()
>{
>  static int counter = 0;
>   counter++;
>}

>This value will be assigned to it only the first time the function is
>called, and it will keep it's value between function calls.  And if I'm not
>mistaken, there will be only one value shared across all instances of this
>class

That's not my understanding of how static would work for this local
variable..... I understood static only worked like that for class
members. Should be easy to test, but I'm too damn idle to write the
code to do it :

We don't keep a dog and bark ourselves round here so.....

"Jim !!! Resolution required here !"

<g>

Bob Moore [MVP]

-----------------------------------------------------------------
Due to an unreasonable amount of queries, I no
longer answer emailed questions. Sorry.
-----------------------------------------------------------------



Sun, 30 Jul 2000 03:00:00 GMT  
 use of static variables in C++ objects

On Wed, 11 Feb 1998 08:04:15 -0800, "Steven Schulze"

Quote:

>You can place static variables inside member functions so that they can only
>be accessed by that function, and you can assign a value to it in the
>function.  Like this:

>void MyClass::Foo()
>{
>  static int counter = 0;

FYI: Initialization with zero isn't necessary; static duration objects
are always zero-initialized, before any other initialization occurs.

Quote:
>   .
>   .
>   counter++;
>}

>This value will be assigned to it only the first time the function is
>called, and it will keep it's value between function calls.  And if I'm not
>mistaken, there will be only one value shared across all instances of this
>class (although I might be wrong about that).

You're right, except you mean "initialized", not "assigned". The above
is no different than defining a static duration variable inside a
non-member function. While often the appropriate choice, there can be
a drawback to static variables of class type defined in block scope.
For a class with a ctor or dtor, the compiler has to maintain some
housekeeping information; typically, the compiler will use a hidden
flag to indicate whether the object has been initialized and added to
the destruction list, that it checks upon each and every entry to the
block. This does slow access down a tiny bit. However, under compilers
that initialize all file scope statics before entry to main, this
typically isn't necessary for objects defined at file scope, including
class statics.

--
Doug Harrison



Mon, 31 Jul 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Using C++ Objects from a non-object-oriented language

2. Lost static variables in VC++ debugger using XP

3. Linking Error when using a static variable from a DLL

4. Getting a Linking error when using a static member variable in a DLL

5. unresolved externals using static variable in class

6. Using Static text variable to change message

7. C++ style static funciton variables?

8. Using static library that includes object from the STL

9. Creating static library that includes object using the STL

10. static functions and non static variables

11. Static variable not instaniated in static library

12. static functions with non-static variables!

 

 
Powered by phpBB® Forum Software