1

I am going through Chapter 17 in the new Stroustrup book and I am confused by initializing a class with an initialization list.

Example:

in .hpp:

class A
{
    public:
        A() : _plantName(std::string s), _growTimeMinutes(int 1);
        virtual ~A();

    private:
        std::string _plantName;
        int _growTimeMinutes;
};

in .cpp:

A::A() : _plantName(std::string s), _growTimeMinutes(int i)
{

}

or is it in .cpp:

A::A(std::string s, int i) : _plantName(std::string s), _growTimeMinutes(int i)
{

}

and calling that:

A a {"Carrot", 10};

I learned c++ back in 1998 and have only programmed in it off and on over the years until recently. How long ago did this stuff change? I know I could still do that the older way but I really want to learn new!

Morwenn
  • 21,684
  • 12
  • 93
  • 152
Jasmine
  • 15,375
  • 10
  • 30
  • 48
  • Those were around long before C++11. The brace initialization wasn't. – chris Jun 02 '13 at 20:34
  • Can you please edit this and try to clarify what your actual question is? The various constructor implementations and declarations are all trying to initialize members that don't exist, so it's hard to tell what part you're asking about. – Ernest Friedman-Hill Jun 02 '13 at 20:36
  • Maybe this will help: http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – chris Jun 02 '13 at 20:39
  • @chris Brace initialization was introduced in C++03, but only on structs and classes that conform to the Plain Old Data (POD) definition. – djf Jun 02 '13 at 20:41
  • @djf, I was kind of thinking that as I said it. I could have said uniform initialization, but then the OP would have no idea what I'm talking about. I figured it would be unambiguous enough since the POD rule can't apply here. – chris Jun 02 '13 at 20:42
  • @chris yeah, I expected you knew. I just put it out for completeness – djf Jun 02 '13 at 20:44
  • @djf In C++03, brace initialization would require an `=` sign. OP's code sample requires C++11. – juanchopanza Jun 02 '13 at 21:20
  • @juanchopanza good point, hadn't noticed that – djf Jun 02 '13 at 21:22

2 Answers2

4

First I think initialization lists are useful when when you are dealing with constant members or when passing objects as parameters since you avoid calling the default constructor then the actual assignement.

You should write the following code in your cpp file : no need to rewrite the parameters types in the initialization list.

A::A(std::string s, int i) : _plantName(s), _growTimeMinutes(i)
{
}

Your h file should be :

class A
{
    public:
         A(std::string, int);
    private:
        std::string _plantName;
        int _growTimeMinutes;
};

And you should create a new A object like that

A new_object("string", 12);
Silouane Gerin
  • 1,201
  • 10
  • 13
  • point of clarification. Stroustrup shows: `double d2 {2.3};` can one not do that for user defined types? thus the way you notated it above? – Jasmine Jun 02 '13 at 20:57
  • 2
    I didn't know that notation : it seems you can write both forms and it'll be equivalent in most cases. You may want to read [this thread](http://stackoverflow.com/questions/9976927/when-to-use-the-brace-enclosed-initializer). – Silouane Gerin Jun 02 '13 at 21:22
  • so now what I am learning is that since _plantName and _growTimeMinutes are part of another class that A() extends I need to think about this different yet – Jasmine Jun 02 '13 at 21:57
1

It should be

A::A(std::string s, int i) : _plantName(s), _growTimeMinutes(i) {

}

for example

supposing the variables _plantName and _growTimeMinutes are declared within class A or one of its superclasses. s and i are the constructor parameters for class A, the initialization will then call the string-constructor for _plantName with argument s and the int-constructor for _growTimeMinutes with argument i, thus initializing both variables.

Initialization lists are especially needed if you want to initialize const references. The assignment within a constructor would not work.

Hope I could help

  • Pedantic, maybe, but it can't call the int constructor because ints don't have one. – chris Jun 02 '13 at 20:38
  • and is the header correct because I read that you dont put it in your header (http://stackoverflow.com/questions/7665021/c-member-initialization-list) but i see other examples that do have it in a header too (http://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c/) – Jasmine Jun 02 '13 at 20:41
  • @chris You're absolutely right. It is not an actual constructor in a technical sense, since int is an integral data type. I used the term for clarification in this case. – voidpointercast Jun 02 '13 at 20:42
  • @Jason, You put it in the definition. – chris Jun 02 '13 at 20:43
  • @chris, Thanks. So member initialization is really beneficial as the object ic created after initialization where as is not the case with member assignment. So in theory I can pass functions and that work is dont first then the object created with the work done. Seems like it could have some speed benefits due to this – Jasmine Jun 02 '13 at 20:51