作为一个c#开发人员,我习惯了遍历构造函数:
class Test {
public Test() {
DoSomething();
}
public Test(int count) : this() {
DoSomethingWithCount(count);
}
public Test(int count, string name) : this(count) {
DoSomethingWithName(name);
}
}
在c++中有办法做到这一点吗?
我尝试调用类名和使用'this'关键字,但都失败了。
测试比决定更容易:)
试试这个:
#include <iostream>
class A {
public:
A( int a) : m_a(a) {
std::cout << "A::Ctor" << std::endl;
}
~A() {
std::cout << "A::dtor" << std::endl;
}
public:
int m_a;
};
class B : public A {
public:
B( int a, int b) : m_b(b), A(a) {}
public:
int m_b;
};
int main() {
B b(9, 6);
std::cout << "Test constructor delegation a = " << b.m_a << "; b = " << b.m_b << std::endl;
return 0;
}
然后用98 std编译:
g++ main.cpp -std=c++98 -o test_1
你会看到:
A::Ctor
Test constructor delegation a = 9; b = 6
A::dtor
所以:)
c++ 11:是的!
c++ 11及以后的版本也有同样的特性(称为委托构造函数)。
语法与c#略有不同:
class Foo {
public:
Foo(char x, int y) {}
Foo(int y) : Foo('a', y) {}
};
c++ 03:不
不幸的是,在c++ 03中没有办法做到这一点,但是有两种方法来模拟这个:
你可以通过默认参数组合两个(或更多)构造函数:
类Foo {
公众:
Foo(char x, int y=0);//结合两个构造函数(char)和(char, int)
/ /……
};
使用init方法共享公共代码:
类Foo {
公众:
Foo (char x);
Foo(char x, int y);
/ /……
私人:
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)
{
/ /……
}
请参阅c++ FAQ条目以获得参考。