views:

10345

answers:

6

Hi *,

As an c# developer I'm used to run through constructors:

class Test {
    public Test() {
        DoSomething();
    }

    public Test(int count) : this() {
        DoSomethingWithCount(count);
    }

    public Test(int count, string name) : this(count) {
        DoSomethingWithName(name);
    }
}

Is there a way to do this in c++ ?

I tried calling the Class name and using the 'this' keyword, but both fails.

A: 

If I understand your question correctly, you're asking if you can call multiple constructors in C++?

If that's what you're looking for, then no - that is not possible.

You certainly can have multiple constructors, each with unique argument signatures, and then call the one you want when you instantiate a new object.

You can even have one constructor with defaulted arguments on the end.

But you may not have multiple constructors, and then call each of them separately.

warren
He's asking if one constructor can call another one. Java and C# allow this.
Jonathan
right - that's not possible in C++
warren
+1  A: 

No, in C++ you cannot call a constructor from a constructor. What you can do, as warren pointed out, is:

  • Overload the constructor, using different signatures
  • Use default values on arguments, to make a "simpler" version available

Note that in the first case, you cannot reduce code duplication by calling one constructor from another. You can of course have a separate, private/protected, method that does all the initialization, and let the constructor mainly deal with argument handling.

unwind
+4  A: 

No, you currently can't call one contructor from another in C++ (delegating constructor, it's called, I think).

This will change in C++ 09, which such syntax (example taken from Wikipedia) :

class SomeType
{
  int number;

public:
  SomeType(int newNumber) : number(newNumber) {}
  SomeType() : SomeType(42) {}
};
ckarmann
+26  A: 

Unfortunately there's no way to do this in C++.

two ways of simulating this:

1) You can combine two (or more) constructors via default parameters:

class Foo {
 public:
   Foo(char x, int y=0);  // combines two constructors (char) and (char, int)
   ...
 };

2) Use an init method to share common code

class Foo {
 public:
   Foo(char x);
   Foo(char x, int y);
   ...
 private:
   void init(char x, int y);
 };

 Foo::Foo(char x)
 {
   init(x, int(x) + 7);
   ...
 }

 Foo::Foo(char x, int y)
 {
   init(x, y);
   ...
 }

 void Foo::init(char x, int y)
 {
   ...
 }

see this link for reference

JohnIdol
Actually remarkably default parameters makes for a _very clean_ way to do what we'd commonly accomplish calling this() in C#
bobobobo
+1  A: 

It is worth pointing out that you can call the constructor of a parent class in your constructor, e.g.:

class A{ .... };

class B: public A { B() : A() { ... do more stuff... } };

But, no, you can't call another constructor of the same class.

kchoose2
+1  A: 

I believe you can call a ctor from a ctor. It will compile and run. I recently saw someone do this and it ran on windows and linux.

It just doesn't to what you want. The inner ctor will construct a temporary local object which gets deleted once the outer ctor returns. They would have to be different ctors as well or you would create a recursive call.

Ref: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3

sqqqrly