作为一个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'关键字,但都失败了。


当前回答

这种方法可能适用于某些类型的类(当赋值操作符表现“良好”时):

Foo::Foo()
{
    // do what every Foo is needing
    ...
}

Foo::Foo(char x)
{
    *this = Foo();

    // do the special things for a Foo with char
    ...
}

其他回答

简单地说,你不能在c++ 11之前。

c++ 11引入了委托构造函数:

Delegating constructor If the name of the class itself appears as class-or-identifier in the member initializer list, then the list must consist of that one member initializer only; such constructor is known as the delegating constructor, and the constructor selected by the only member of the initializer list is the target constructor In this case, the target constructor is selected by overload resolution and executed first, then the control returns to the delegating constructor and its body is executed. Delegating constructors cannot be recursive. class Foo { public: Foo(char x, int y) {} Foo(int y) : Foo('a', y) {} // Foo(int) delegates to Foo(char,int) };

请注意,委托构造函数是一个全有或全无的提议;如果一个构造函数委托给另一个构造函数,则调用构造函数的初始化列表中不允许有任何其他成员。如果只初始化一次const/reference成员,这是有意义的。

是或否,取决于c++的版本。

在c++ 03中,不能从一个构造函数调用另一个构造函数(称为委托构造函数)。

这在c++ 11(又名c++ 0x)中改变了,它增加了对以下语法的支持: (例子摘自维基百科)

class SomeType
{
  int number;
 
public:
  SomeType(int newNumber) : number(newNumber) {}
  SomeType() : SomeType(42) {}
};

在Visual c++中,你也可以在构造函数中使用这个符号:this->Classname::Classname(另一个构造函数的参数)。请看下面的例子:

class Vertex
{
 private:
  int x, y;
 public:
  Vertex(int xCoo, int yCoo): x(xCoo), y(yCoo) {}
  Vertex()
  {
   this->Vertex::Vertex(-1, -1);
  }
};

我不知道它在其他地方是否可以工作,我只在Visual c++ 2003和2008中测试过。我想,您也可以这样调用几个构造函数,就像在Java和c#中一样。

附言:坦率地说,我很惊讶之前没有提到这一点。

我建议使用private friend方法,该方法实现了构造函数的应用逻辑,并由各种构造函数调用。这里有一个例子:

假设我们有一个名为StreamArrayReader的类,它带有一些私有字段:

private:
    istream * in;
      // More private fields

我们想定义两个构造函数:

public:
    StreamArrayReader(istream * in_stream);
    StreamArrayReader(char * filepath);
    // More constructors...

其中第二个简单地使用了第一个(当然,我们不想复制前者的实现)。理想情况下,人们会做如下事情:

StreamArrayReader::StreamArrayReader(istream * in_stream){
    // Implementation
}

StreamArrayReader::StreamArrayReader(char * filepath) {
    ifstream instream;
    instream.open(filepath);
    StreamArrayReader(&instream);
    instream.close();
}

然而,这在c++中是不允许的。因此,我们可以定义一个私有友方法,如下所示,它实现了第一个构造函数应该做的事情:

private:
  friend void init_stream_array_reader(StreamArrayReader *o, istream * is);

现在这个方法(因为它是一个朋友)可以访问o的私有字段。然后,第一个构造函数变成:

StreamArrayReader::StreamArrayReader(istream * is) {
    init_stream_array_reader(this, is);
}

注意,这不会为新创建的副本创建多个副本。第二个是:

StreamArrayReader::StreamArrayReader(char * filepath) {
    ifstream instream;
    instream.open(filepath);
    init_stream_array_reader(this, &instream);
    instream.close();
}

也就是说,不是一个构造函数调用另一个构造函数,而是两个构造函数都调用一个私有友元!

这种方法可能适用于某些类型的类(当赋值操作符表现“良好”时):

Foo::Foo()
{
    // do what every Foo is needing
    ...
}

Foo::Foo(char x)
{
    *this = Foo();

    // do the special things for a Foo with char
    ...
}