|
humm, what about when the function returns p directly, instead of its address ?
i know i already encountered such a problem, but i don't remember the exact case...
|
|
|
|
|
Same result, this code:
<br />
int create2( )<br />
{<br />
static int p = 0;<br />
p++; <br />
return p;<br />
}<br />
<br />
::printf("%d %d", create2(), create2());<br />
produces the result:
2 1
Same as before. A breakpoint at "return p" gives the same result as before (of course).
Have you tried it yourself?
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hi kakan
isnt that what you would expect because the definition of a static variable is to create 1 and only 1 version of the variable?
|
|
|
|
|
Hello. That's exactly what I expect. But tox obviously doesn't. And it seems as it's my job to prove him wrong, instead of the opposite...
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Sorry, may be I am getting confused.... Um, since at every funcion call to create2() you set p = 0 it should reset p everytime. But appears to not. Why is that?
Thanks for any information.
|
|
|
|
|
Sorry, may be I am getting confused In your definition of create2() you reset p, by p = 0. So in each call to create2() p should be set to 0 before being incremented. So wouldnt you therefore expect the same value printed everytime?
|
|
|
|
|
Sorry, may be I am getting confused In your example above of create() you reset p, by p = 0. So in each call to create() p should be set to 0 before being incremented. So wouldnt you therefore expect the same value printed everytime?
|
|
|
|
|
Actually I just answered my own question
The static int p = 0 only gets called once at instantiation which makes sense coz otherwise you will have loads of p's around which goes against the idea of it being static (only 1 copy of the variable exists).
Well I hope that is correct.
|
|
|
|
|
Sorry for the delay. It's due to the fact I went home from work yesterday.
That's right, a static variable just gets initialized once.
Besides, there are just one copy of it, in the heap.
The fact that it is stored in the heap (as opposite to the stack), makes it protected from alterations.
So if you want to make sure (as in this case) that a value, returned from a function will contain the same value, static is the way to go.
This goes for plain C. For C++, a class member variable does the same.
Kakan
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hello
Are you sure that static members are on the heap? That would imply they are dynamically allocated would'nt it? Also if you have an item on the heap you would need to use 'delete' at some point for clean up. If you can send me a reference for this I would be most interested.
Thanks !
|
|
|
|
|
Sorry, I was wrong about this, it seems.
The most common place to store static variabels seems to be in the data segment of the program.
Sorry.
But the rest is true!
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
I did a google on 'static heap C++ allocation'. It seems on some systems its on the heap and others on the stack. But it is allocated at run time.
|
|
|
|
|
kakan wrote: Correct me if I'm wrong, but as far as I remember, a static variable gets a fixed position on the heap.
Nope. Not on the heap.
|
|
|
|
|
Add the following to your test:
void effect( void )
{
int change = 1;
}
int main( )
{
int *p;
p = create();
effect();
cout ...
...
}
...cmk
Save the whales - collect the whole set
|
|
|
|
|
Hi how are you ?
How can i get the caption of a bouton is it possible
thanks
zizzzzzz
|
|
|
|
|
zizzzz wrote: caption of a button
How about using GetWindowText()?
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
zizzzz wrote: Hi how are you ?
fine since last day... what about you ?
zizzzz wrote: How can i get the caption of a bouton is it possible
sure it's possible... everything's possible in C++.
CString strCaption = _T("");
CButton* pMyButton = NULL;
pMyButton = (CButton*)GetDlgItem(IDC_MY_BUTTON);
pMyButton->GetWindowText(strCaption);
|
|
|
|
|
I m fine too !
I hope that there is nothing wrong with you
thank's
zizzzzzzzzzzz
|
|
|
|
|
What does _T mean in your code:
CString strCaption = _T("");
How would the following differ?
CString strCaption = "";
Thanks
|
|
|
|
|
it means that in ANSI, the string will be seen as "" (const char* ), but in UNICODE, the string will be seen as L"" (const wchar_t* ).
|
|
|
|
|
I know you already have the answer to your question, which is nice.
You really need to have a better title for your post than "o".
If someone hadn't already replied, I'd have ignored your message completely.
Iain.
|
|
|
|
|
I'm really sorry i didn't mean it because I left the title to
the end and then i hit the bouton post message without checking out
I forget it
sorry !
|
|
|
|
|
but you still can use the <small>[Modify]</small> link, even when posted
|
|
|
|
|
iam looking for ways to convert an ActiveX DLL created with MFC into an MFC executable application. Wanted to know the various settings changes that needs to be done in visual studio project settings. How to change the entry point function from DLLMain() to that of an WinMain()?
Thanks and Regards,
Pradeep
|
|
|
|
|
An activeX is contained in an application that interracts with it. If you have a standalone ActiveX, that's useless. Take for example the grid control: what's the purpose of having a 'standalone' grid that does nothing, except that you are able to write things in the cells ?
Why do you want to do such a thing anyway ?
|
|
|
|