views:

144

answers:

4

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?

A: 

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

There is nothing we can do
But will be in C++0x (delegating constructors) ... :)
UncleBens
+1 for Uncle ;)
There is nothing we can do
@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
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.
dauphic
Herb Sutter works for Microsoft and he was the chair for a long time.
graham.reeds
@A-ha: Your answer isn't inappropriate, but your language is.
Ben Voigt
+3  A: 

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)
{}
GMan
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
I let myself fix the use of default arguments.
Benoît
@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.
GMan
@GMan: ah yes! This and other of your comments have cleared it up some. Thanks.(and I'll get a book or two)
Peter Stewart
+1  A: 

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
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
+1  A: 

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