C++这个在构造函数中?

nob*_*ody 0 c++ java constructor this

可能重复:
构造函数中的c ++调用构造函数

如何在c ++中做"自我"(这个)作业?

Java的:

 public Point(Point p) {
        this(p.x, p.y);
    }
Run Code Online (Sandbox Code Playgroud)

在C++中如何做到这一点?

它会是相似的this->(constructor of point that takes x, constructor of point that takes y);吗?

bdo*_*lan 8

在C++ 0x中,您可以使用委托构造函数:

Point(const Point &p) : Point(p.x, p.y) { }
Run Code Online (Sandbox Code Playgroud)

请注意,没有编译器完全支持C++ 0x; 这个特殊功能尚未在G ++中实现.

在旧版本的C++中,您必须委托私有构造函数:

private:
    void init(int x, int y) { ... }
public:
    Point(const Point &p) { init(p.x, p.y); }
    Point(int x, int y)   { init(x, y); }
Run Code Online (Sandbox Code Playgroud)

  • 虽然在这种情况下,它可能不值得麻烦. (2认同)