Behaviour I can not explain. 
Author Message
 Behaviour I can not explain.

Hi,

The case is. I have created a control via subclassing a CDialog. Lets call
it MyDialog. I Create multiple instances of MyDialog and put them on a CView
via the Create function of MyDialog. Now the problem: If I use a "static int
myVar" inside a function the variable myVar memory space is used by all
instaces of MyDialog. They overwrite eachother. I think this is strange....

Code example:

void MyDialog::Func1()
{
   static int myVar;
    /* ..... code ...... */

Quote:
}

Can someone explain why this is happening?

Dennis.



Mon, 12 Sep 2005 11:22:38 GMT  
 Behaviour I can not explain.
This behaviour is not strange at all.
It's completely according to the C++ standards.
A static declared member variable is shared among the
instances of a class.
If you declare a static variable in a function, the
variable will keep its value between calls, but it will
also be shared among the instaces of the class.

The memory for member functions are considered "static",
i.e. all instances share the same function.
If you declare a static variable in a member function, the
variable will be stored together with the code of the
member function and not together with one particular
instance.

If you don't want this behaviour, make the variable an
ordinary member variable.

--
Rog

Quote:
>-----Original Message-----
>Hi,

>The case is. I have created a control via subclassing a
CDialog. Lets call
>it MyDialog. I Create multiple instances of MyDialog and
put them on a CView
>via the Create function of MyDialog. Now the problem: If
I use a "static int
>myVar" inside a function the variable myVar memory space
is used by all
>instaces of MyDialog. They overwrite eachother. I think
this is strange....

>Code example:

>void MyDialog::Func1()
>{
>   static int myVar;
>    /* ..... code ...... */
>}

>Can someone explain why this is happening?

>Dennis.

>.



Mon, 12 Sep 2005 12:01:29 GMT  
 Behaviour I can not explain.
Thank you,

Now I can see the logic behind it.

Greetings Dennis.


Quote:
> This behaviour is not strange at all.
> It's completely according to the C++ standards.
> A static declared member variable is shared among the
> instances of a class.
> If you declare a static variable in a function, the
> variable will keep its value between calls, but it will
> also be shared among the instaces of the class.

> The memory for member functions are considered "static",
> i.e. all instances share the same function.
> If you declare a static variable in a member function, the
> variable will be stored together with the code of the
> member function and not together with one particular
> instance.

> If you don't want this behaviour, make the variable an
> ordinary member variable.

> --
> Rog

> >-----Original Message-----
> >Hi,

> >The case is. I have created a control via subclassing a
> CDialog. Lets call
> >it MyDialog. I Create multiple instances of MyDialog and
> put them on a CView
> >via the Create function of MyDialog. Now the problem: If
> I use a "static int
> >myVar" inside a function the variable myVar memory space
> is used by all
> >instaces of MyDialog. They overwrite eachother. I think
> this is strange....

> >Code example:

> >void MyDialog::Func1()
> >{
> >   static int myVar;
> >    /* ..... code ...... */
> >}

> >Can someone explain why this is happening?

> >Dennis.

> >.



Mon, 12 Sep 2005 12:26:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: GetParent() weird behaviour (please, explain)

2. Q: GetParent() weird behaviour (please, explain)

3. please explain:T* pT = static_cast<T*>(this)......,does not call virtual function

4. Determining what canned preprocessor symbols are available

5. Beginner [Q] Using Canned File Open/Save dialog

6. Help with compiling a "canned" program

7. Canned Dialogs

8. Undefined behaviour - or not???

9. Please explain ?

10. Can someone explain this line to me

11. Could someone explain linker warning LNK243?

12. Please explain the diference @Assembly directive and Assembly class

 

 
Powered by phpBB® Forum Software