Oh you *must* use the scope resolution operator to access members of a class. But not just ordinary members. STATIC members only.
But for normal accessing, we use also -> and . (dot).
Let's find out about statics now:
Rules:
- A
decleration of static members in a class means NOT that it is defined.
It is only there for us humans to see that it is a static member. This is called "logical presence" instead of "physical presence".
- You must
define the static member OUTSIDE the class, on a global level.
Usually, immediatly after the definition of the class is the place to do so.
- A static member WILL exist BEFORE any object (instance of a class) exists.
- If statics are not initialized, the standard dictates that the static will automatically initialized with the value "0". For std::string this means an empty string.
- All instances of a class will share the same static member.
Observe how I use the :: operator to define the static just outside the class.
A demonstration follows below:
Code:
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
class ClassWithAStatic
{
public:
static string theString;
inline string show_static()
{
return theString;
}
};
//Must declare out of class!
string ClassWithAStatic::theString("I Live before all.");
int main()
{
ClassWithAStatic a, b;
cout<<a.show_static()<<endl;
cout<<b.show_static()<<endl;
//Lets redefine the static member. Notice that all objects adapt it's value.
ClassWithAStatic::theString = "I am reborn for all.";
cout<<a.show_static()<<endl;
cout<<b.show_static()<<endl;
return 0;
}
However, I didn't want to mention this for falsepride. This should come a week later if he studies seriously.