1

I was writing some java last night and I had two constructors that looked basically alike, except my default constructor provided some values to my object, it was something like this:

testObject(){
     width=5;
     height=12;
     depth=7;
     //other stuff is the same as the next one
}

testObject(int x, int y, int z){
    width=x; 
    height = y;
    depth = z;
    //All the other stuff is the same as default
}

So in this case, I was able to convert the code to do this instead:

testObject(){
    this(5,12,7);
}

That sent the values from the default constructor back to the constructor as the 3-int constructor to be built as such. Is there any way to get this type of functionality in C++?

HunderingThooves
  • 962
  • 8
  • 20
  • 33
  • Not yet, but there will be! See this [link](http://publib.boulder.ibm.com/infocenter/lnxpcomp/v111v131/index.jsp?topic=/com.ibm.xlcpp111.linux.doc/language_ref/delegating_ctors.html). – Daniel Walker Sep 14 '11 at 20:37

6 Answers6

5

In C++0x, you can do this:

TestObject() :TestObject{5, 12, 7} {}

See Delegating Constructors for more details. (Curly braces not look familiar to you? They're for preventing narrowing.)

If you don't have C++0x available yet, then in your case, you can use default arguments as mentioned in other answers here.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
1

You were close, try calling the 3-parameter constructor in your initializer list for your default constructor.

testObject(int x, int y, int z) :
    width(x), 
    height(y),
    depth(z) {
    //All the other stuff is the same as default
}

testObject() : testObject(5,12,7) {
     //other stuff is the same as the next one
}
AJG85
  • 15,849
  • 13
  • 42
  • 50
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
0

There is no way to do this, but you can create a private method with initialization code and call it from your different constructors.

EDIT: duplicate of Can I call a constructor from another constructor (do constructor chaining) in C++?

Community
  • 1
  • 1
Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
0

You can use default values in the constructor (i.e. testObject(int x = 5, int y = 12, int z = 7) ) to do what you want.

goffrie
  • 461
  • 2
  • 7
0

The most interesting thing is that you don't need to do that. Default arguments are aimed at solving problems like this. Here is an example:

#include <iostream>

struct Foo
{
    int x, y, z;

    Foo (int x = 5, int y = 12, int z = 7)
        : x (x), y (y), z (z)
    {}
};

int
main ()
{
    Foo f1, f2 (1, 2, 3);
    std::cout << f1.x << ", " << f1.y << ", " << f1.z << '\n'
         << f2.x << ", " << f2.y << ", " << f2.z << '\n';
}

Just for example sake, you can call a constructor of a class using placement new, like this:

struct Foo
{
    int x, y, z;

    Foo (int x, int y, int z)
        : x (x), y (y), z (z)
    {}

    Foo ()
    {
        this->~Foo ();
        new (this) Foo (5, 12, 7);
    }
};

This feature also comes out of the box in C++11, you can read more here.

  • It's undefined behavior in general to call the destructor from the constructor, since the object's lifetime hasn't begun yet. In this case, though, the class has a trivial destructor so it might be valid. I'm not sure. As you say, though, it's for example only not real use -- there are enough other problems aside from that one. – Steve Jessop Sep 14 '11 at 20:53
  • @Steve: Yeah, this one is just an example. It will work though, if you a lot of conditions are met (like in a perfect storm :-)). For instance, this cannot be done if this class is a base class and has a virtual destructor etc. In this case destructor shouldn't be called, and example is for example sake, because people rarely come up with crazy ideas like this. Like they say in some videos - do not try to repeat this at home! :) –  Sep 15 '11 at 00:00
0

You can add default variable values to your class.

testObject::testObject(int x, int y, int z = 5)

Then you can call it with testObject(1,2) and z will retrieve the default value of 5

Rontologist
  • 3,568
  • 1
  • 20
  • 23