9

Should I declare the member initializer list for a class in the constructor declaration:

class A
{
public:
    A(int data) : theData(data);
};

or in the constructor definition:

A::A(int data) : theData(data)
{
    // code...
};

or does it not matter? If you do it a certain way, why?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    You swapped definition <-> declaration. The former is a declaration, the latter is a definition. – leemes Nov 04 '12 at 17:04
  • 1
    -1 because this is trivial to (a) look up in your book, and (b) try out for yourself in two minutes flat. We expect some level of prior research here at Stack Overflow, which you should know after almost two years. – Lightness Races in Orbit Nov 04 '12 at 17:04
  • "Initialiser list" usually means other things (things that have `{}` around them). Say "member initialiser [list]" or "ctor-initialiser". – Lightness Races in Orbit Nov 04 '12 at 17:06
  • @LightnessRacesInOrbit Just picking up C++, thank you very much. I was just wondering if there was even a way to do it in the declaration. –  Nov 04 '12 at 17:08
  • @leemes: "member initiali[z]er" is not a "wrong term" - it's the terminology used in the standard. By contrast, grepping it for your "initialization list" (even with "member" omitted) yields zero results. – Lightness Races in Orbit Nov 04 '12 at 17:20
  • @LightnessRacesinOrbit Hmm,fair enough, my mistake. I'm used to the term "Constructor initialization lists" and short "init-list". And "initializer-list" as the term for literal arrays (`{1,2,3}`). I think it's a bit confusing. – leemes Nov 04 '12 at 17:44
  • @leemes: Yes, we should avoid "initializer list". The prefix "member" disambiguates it. I do prefer "ctor-initializer" but it's not as accessible. Admittedly I should probably drop the trailing "list" to make it just "member initializer". – Lightness Races in Orbit Nov 04 '12 at 18:03

1 Answers1

12

You can only have member initializer lists in definitions of your constructors. It is a part of the definition.

leemes
  • 44,967
  • 21
  • 135
  • 183