static class members of the latest created class instance
I'd like to access non-static class members out of a static method. It is also defined which instance of the class should be used for the access. It is the instance, that has been created most recently.
I try to do this by using a static pointer as a member of that class, which indicates, what was the object inastatiated most recently. It is intended that the class constructor sets this pointer every time we instatiate an object.
Code looks as follows:
class Klasse
{
public:
Klasse()
{
me = this; //an error is thown here:"undefined reference to `Klasse::me'"
nummer = eins;
}
private:
static Klasse* me;
enum
{
eins = 1,
zwei = 2
}nummer;
static void SMethod()
{
me->nummer= zwei;
}
};
int main(void)
{
int i = 0;
Klasse instanz;
//...
}
Has anyone an idea, why the error "undefined reference to `Klasse::me'" is thrown?
You missed this:
Klasse* Klasse::me = nullptr;
static data member must be defined in .cpp file.
链接地址: http://www.djcxy.com/p/96378.html上一篇: 静态成员函数作为函数的指针?
下一篇: 最新创建的类实例的静态类成员