Skip to main content
gamedev.net
🔒 Locked

using static var from function outside the functionbody

Started by Leadorn 1 reply 300+ views
Original Post
Leadorn
Leadorn
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?
Jingo
Jingo
Not really. You may get problems doing that in destructors of global or other static objects.
Qw3r7yU10p!
Qw3r7yU10p!
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.