比方说,我有一个简单的代码:
Main.cpp的
#include "A.h"
// For several reasons this must be a global variable in the project
A a1;
int _tmain(int argc, _TCHAR* argv[])
{
// Another stuff
return 0;
}
啊
#pragma once
#include
class A
{
private:
// The following works normal if we use simple types like int and etc.
static std::string myString;
public:
A();
};
A.cpp
#include "stdafx.h"
#include "A.h"
// This executes after A::A(), so we are losing all the modifyed content
// If we skip the ="test" part, the string is going to be empty
std::string A::myString = "test";
A::A()
{
// Here myString == ""
myString += "1";
}
问题很明显:在这种情况下,我不能在类A的构造函数中使用静态变量,因为它们不保存更改.虽然我需要它们才能处理一些数据.
拜托,建议我一个解决方案.