Ram wrote:
Yes. The fact that i is declared in a class method instead of a global
function changes nothing about its behavior.
A further question on this. Local static variables inside class member
functions are instantiated per object or per class? g++ (2.96)
instantiates it per class i.e. if I say
A a1, a2;
a1.ff();
a2.ff();
I found that the value persists between the two calls. Is this also
required by the standard?
Ramashish
they are instantiated per class (first time the method is call,
regardless of the instantiated number of objects of that type). If you
want it to be instantiated on a per object basis, make the variable a
class level, member variable not a static variable inside the method.
class A
{
private:
int m_var; // instantiated per object
public:
A():m_var(0){}
void f()
{
static int x = some_function_returning_an_int(); // instantiated
per class
}
};
/dan