Reputation: 3073
From this article "Initializing C++ Class Members"
It says: There's simply no other way to pass the argument to m_member
which means we cannot. But I don't understand we cannot write:
class CMyClass {
CMember m_member(2);
public:
CMyClass();
};
In this article: How do C++ class members get initialized if I don't do it explicitly?
It has a constructor that allows you specify its initial value CDynamicString(wchat_t* pstrInitialString).
To 'hard code' the registry key name to which this writes you use braces:
class Registry_Entry{
public:
Registry_Entry();
~Registry_Entry();
Commit();//Writes data to registry.
Retrieve();//Reads data from registry;
private:
CDynamicString m_cKeyName{L"Postal Address"};
CDynamicString m_cAddress;
};
From my own test, we cannot use
CMember m_member(2);
in the header file. But why?
And how come
CDynamicString m_cKeyName{L"Postal Address"};
can work? (BTW, I suspect there was a typo. It should remove "L".
Upvotes: 3
Views: 107
Reputation: 1974
Before C++11, the only way to initialise a non-static member of a class was in a constructor of that class, not in the class definition.
C++11 has relaxed some of those restrictions (and introduced initialiser lists, which you're using to initialise CDynamicString) but not all of them. Not all compilers have caught up with the changes either. Hence some of the apparent anomalies you see.
Upvotes: 2