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++?
In C++0x, you can do this:
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.