0

Is it proper to initialize my static member variable in the constructor class?

// CFoo.h
class CFoo
{
public:
    CFoo();
    ~CFoo();
    static std::string str;
};

// CFoo.cpp
CFoo::CFoo()
{
    str = "HELLO";
}

CFoo::~CFoo()
{
}

Thanks

domlao
  • 15,663
  • 34
  • 95
  • 134

1 Answers1

1

You haven't define static member yet. You need to define it in CFoo.cpp.

CFoo.cpp

std::string CFoo::str;  // define str

CFoo::CFoo()

{
    str = "HELLO";  // reset str is fine
}

CFoo::~CFoo()
{
}
billz
  • 44,644
  • 9
  • 83
  • 100