using static var from function outside the functionbody

Started by
1 comment, last by Qw3r7yU10p! 19 years, 5 months ago
Im using a static variable from a funktion outside the funktion.
[SOURCE]
int *foo()
{
 static int i;
return &i;
}

int *i = foo();
*i = 1;

[/SOURCE]
Is there any danger doing this?
Advertisement
Not really. You may get problems doing that in destructors of global or other static objects.
Dangers:

1) Obvious: Don't try and delete the returned value. Return a reference instead to indicate the function owns it.

2) Pernicious: Global data can destroy your design.

This topic is closed to new replies.

Advertisement