
c++ call constructor from constructor
By: a guest on
Feb 28th, 2012 | syntax:
None | size: 1.18 KB | hits: 15 | expires: Never
class Test {
public Test() {
DoSomething();
}
public Test(int count) : this() {
DoSomethingWithCount(count);
}
public Test(int count, string name) : this(count) {
DoSomethingWithName(name);
}
}
class Foo {
public:
Foo(char x, int y=0); // combines two constructors (char) and (char, int)
...
};
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)
{
...
}
class Foo {
public:
Foo(char x, int y) {}
Foo(int y) : Foo('a', y) {}
};
class SomeType
{
int number;
public:
SomeType(int newNumber) : number(newNumber) {}
SomeType() : SomeType(42) {}
};
class Foo() {
Foo() { /* default constructor deliciousness */ }
Foo(Bar myParam) {
new (this) Foo();
/* bar your param all night long */
}
};
class Foo {
int d;
public:
Foo (int i) : d(i) {}
Foo () : Foo(42) {} //new to c++11
};
class Foo {
int d = 5;
public:
Foo (int i) : d(i) {}
};