0

I just asked this question and the good answers mentioned using an initialization list. So I looked it up in many various places. It was often said that one can use an initialization list to select which constructor to use.

class First
   {private: 
         int a, b, c;
  public:
First(int x);
First(int x, int y);
}

First::First(int x, int y, int z = 0)
{ /* this is a constructor that will take two or three int arguements. */ }

First::First(int x, int y = 0, int z = 0)
{ /* and this will be called if one arguement is given */ }

I thought all assignments should be avoided, so how would I write the initializer lists for these two constructors?

Community
  • 1
  • 1
Peter Stewart
  • 2,857
  • 6
  • 28
  • 30
  • 1
    Is the above legal C++? If I were to use new First(1, 2, 3), wouldn't both constructors be suitable leading to a problem? – Will A Aug 08 '10 at 19:26
  • 1
    You're confusing `initialization lists` with `default parameters` for functions... – Eugen Constantin Dinca Aug 08 '10 at 19:30
  • I am confused. can you recommend a good description of initialization lists? – Peter Stewart Aug 08 '10 at 19:57
  • @Peter: You should get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Nothing else will substitute. – GManNickG Aug 08 '10 at 19:59
  • An initializer list is part of the constructor body. It allows you to 'override' how member variables are initialized. By default, all member variables are initialized with their default constructors. See the answer from GMan for an example, he uses an initializer list to initialize a, b, and c with x, y, and z respectively. – Collin Dauphinee Aug 08 '10 at 20:00
  • @GMan: I just ordered Stroustrup from the library. Thanks – Peter Stewart Aug 08 '10 at 20:36
  • @Peter: Don't forget initialisation lists must be in the order they are listed to in the class. If not strange this might occur! – graham.reeds Aug 09 '10 at 07:49

4 Answers4

3

I'm not quite sure I follow. As it stands, by providing an argument to x and y (and therefore z), both constructors will be available to call, resulting in ambiguity.

I think what you're looking for is:

class First
{
public:
  First(int x);
  First(int x, int y, int z = 0);
};    

// invoked as First f(1);
First::First(int x) :
a(x), b(0), c(0)
{}

// invoked as First f(1, 2); or First f(1, 2, 3);
First::First(int x, int y, int z) :
a(x), b(y), c(z)
{}
Benoît
  • 16,798
  • 8
  • 46
  • 66
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • Why wouldn't you initialize c(0) in the second example? I understood that using assignment (int z = 0 ) was expensive, and that the initialization list was the preferred way. – Peter Stewart Aug 08 '10 at 20:05
  • I let myself fix the use of default arguments. – Benoît Aug 08 '10 at 20:28
  • 1
    @Benoit: Thanks. @Peter: That's not assignment, it's a default parameter. It says "if this parameter isn't provided, use this value". That has nothing to do with assignment, it just uses `=` as a symbol. – GManNickG Aug 08 '10 at 20:51
  • ah yes! This and other of your comments have cleared it up some. Thanks.(and I'll get a book or two) – Peter Stewart Aug 08 '10 at 21:55
1

Ctor initialization list isn't for the purpose of choosing which ver of ctor will be chosen.

There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
  • 3
    But will be in C++0x (delegating constructors) ... :) – UncleBens Aug 08 '10 at 19:39
  • @Uncle the only problem I see with this whole business of C++0x (for which I cannot wait!) is that:1. Most popular IDE namely VS since last release just sucks (due to the fact that it has been written in MJ). 2. I'm not quite sure but guys from Microsoft are probably more interested in their own child (MJ) than Bjarne's and I'm affraid that they are giving full support in VS2010 for MJ and C++ is treated as if it were unwanted gift they recieved and don't know how to say that they really do not like it (the gift I mean.). So when and if they implement C++0x is one big '?'. – There is nothing we can do Aug 08 '10 at 19:51
  • They definitely will be implementing C++0x. Microsoft even sends people to the ISO meetings. Visual Studio's development team isn't just one team, there are people dedicated solely to working on the C++ side of Visual Studio. The 0x features Visual Studio currently supports were chosen because of how easily they could be implemented; if they had implemented more complicated features, I doubt they would have been able to make the release date. I do recall that there is one 0x feature they are refusing to implement, but I don't remember which, only that it wasn't very important. – Collin Dauphinee Aug 08 '10 at 19:58
  • Herb Sutter works for Microsoft and he was the chair for a long time. – graham.reeds Aug 08 '10 at 21:04
  • @A-ha: Your answer isn't inappropriate, but your language is. – Ben Voigt Aug 08 '10 at 21:13
1

This is not how things work. If you want to use default arguments, use only one constructof, declare it with default arguments, and define it (without redefining them).

class First
{
  private: 
         int a, b, c;
  public:
First(int x, int y = 0, int z = 0);
};

First::First(int x, int y, int z)
{   /*...*/ }

Considering your question, i am not sure you know what an initialisation list is...

Benoît
  • 16,798
  • 8
  • 46
  • 66
  • yes, I might have been hasty posing this question. I've been searching "initialization", and have gotten many examples, but no good description/explanation. Thanks – Peter Stewart Aug 08 '10 at 19:55
1

I think you mean this:

class First
{
private: 
    int a, b, c;
public:
    First(int x);
    First(int x, int y);
}

class Second
{
private:
Second(int x) : First(x) { }
Second(int x, int y) : First(x, y) { }
}    

Maybe?

graham.reeds
  • 16,230
  • 17
  • 74
  • 137