A (newbie) C++ static attribute query...
To wit, is the value of a static attribute maintained when there are no objects of the class within which the static attribute is declared?
I've searched, but I can't find a definitive yes or no. A quick experiment says yes, and common sense says yes, but is tempered by my experience that common sense often isn't when dealing with C++' foibles...
I've searched, but I can't find a definitive yes or no. A quick experiment says yes, and common sense says yes, but is tempered by my experience that common sense often isn't when dealing with C++' foibles...
Code:
#include <stdio.h>
class A
{
private:
static int m_i;
public:
A( void )
{
m_i ++;
printf( "%d\n", m_i );
return;
};
};
int A::m_i = 0;
int main( void )
{
A *a;
a = new A();
// This should print "1".
delete a;
// Is the value of m_i maintained whilst there are no objets of A?
a = new A();
// If it is, this should print "2".
delete a;
return 0;
}Mark Bishop
The answer is yes.
A class' static variable can be thought of as a global variable held within the scope of the class.
Thanks TomorrowPlusX, akb825!
Mark Bishop

