Advertisement

Deleting static variables...

Started by February 04, 2000 06:24 AM
2 comments, last by Someone 24 years, 7 months ago
Hi! I''m just wondering if it''s possible to delete a static variable declared in a certaion function from another function? Example: void NAMEOFCLASS::FunctionA() { static float number; ... } NAMEOFCLASS::~NAMEOFCLASS() // Destructor { //suppose to remove the static ''number'' in FunctionA } Is this possible?
It''s not possible. The variable''s scope is limited to the function it is defined in. This does not mean, that the variable looses it''s value, but the variable''s name is only known in the function which declares it.

With member variables and static variables with scope to a source it is quite equal

A static variable declared in the scope of a source file can not be seen in any other source file. This is perhaps the image you have to see except that your scope is limited to functions.

A member variable also retains it''s value between member-function calls. So this is the thing you would probably like to use in your case (private or protected).

Perhaps I totally misunderstand your problem but what would happen in your code if you had 2 Objects of type NAMEOFCLASS ? Then for each object on destruction the destructor is called. The variable number now exists for the funtion, meaning for all objects exatly once.
Another Question: Why would you want to do it like that ?

NextS
Advertisement
Well, I was thinking of putting a static array that is only used by the function alone to do some processing. So, basically, when I no longer need that object, it''ll do the proper memory cleaning and remove that static array as well if it''s no longer used. Or does it get removed automatically?
static variables are stored right there in the EXE. they are never allocated or deleted, they are just "there" for the duration of the program. think of it this way: memory is allocated for them during the compilation/linking stage and they are shoved into the EXE.

static variables inside functions are the same as static functions in a module (source file). except that their name is only known to that function. the variables themselves always "exist" throughout the entire execution of the program.

This topic is closed to new replies.

Advertisement