I have class with static string variable m
.
class turbo
{
public:
static string m;
}
if I not declare like defined below I will get error undefined reference turbo::m
.
string turbo::m;
Why I should declare it twice? I don't need to declare non static variables second time. Where is logic?
To be honest, I was surprised by this too the first time,
but found the very reasonable reason for it.
while member variables are stored within the object allocated space, static members are not placed within the object memory space (either heap or stack)
but have a private shared space. just like functions do.
Therefore, declaring the static string m
within the class. is just like saying: there is a shared definition of this somewhere and the name is turbo::m
.
Hope this clears things up.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加