作为C#开发人员,我习惯于运行构造函数:
class Test {
public Test() {
DoSomething();
}
public Test(int count) : this() {
DoSomethingWithCount(count);
}
public Test(int count, string name) : this(count) {
DoSomethingWithName(name);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在C++中执行此操作?
我尝试调用类名并使用'this'关键字,但都失败了.
class Foo {
public:
Foo() { Foo(1)}
Foo(int x, int y = 0):i(x) {}
private:
int i;
}
Run Code Online (Sandbox Code Playgroud)
谁能给我一些理由我可以这样做吗?如果不是为什么?