3

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".

Community
  • 1
  • 1
user1914692
  • 3,033
  • 5
  • 36
  • 61
  • Definitions for `CMember` and `CDynamicString`? – John Dibling Jan 29 '15 at 17:38
  • @JohnDibling I copy from the two links mentioned in my post. I think here their concrete definition doesn't matter much. Or you could read more details in their original posts. I find if I use if I use "{}", such as CMember m_member{2}; then I can initialize object members in the header file. but I don't know why? – user1914692 Jan 29 '15 at 17:45
  • "It should remove "L"." - no, that makes it a wide string, i.e. an array of `const wchar_t`, to match the `wchar_t*` argument (although that needs `const` to be usable with a literal). – Mike Seymour Jan 29 '15 at 18:02

1 Answers1

2

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.

Rob
  • 1,966
  • 9
  • 13
  • But here I don't understand, CMember m_member(2); cannot work, but CMember m_member{2}; can work. The rule seems a little weird here. And where is the link for this part of grammar? – user1914692 Jan 29 '15 at 17:47
  • 1
    @user1914692: The grammar is in appendix A of the standard. Look for *member-declarator*, which can have a *brace-or-equal-initializer*, but not any other form of initialiser. I don't know why other forms of initialisation aren't allowed; perhaps because the use of `()` might open up new forms of the Most Vexing Parse. – Mike Seymour Jan 29 '15 at 18:00
  • @user1914692: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf, I _think_ § 12.6.2/5 is the relevent section – Mooing Duck Jan 29 '15 at 18:02
  • Thanks. This is helpful. Although a quick browse doesn't lead me to find the concerned information (why use {} instead of () when call the constructor in the initialization). But let me take more time on searching it. – user1914692 Jan 29 '15 at 18:24