java - C++ this 在构造函数中?

标签 java c++ constructor this

<分区>

Possible Duplicate:
c++ call constructor from constructor

如何在 C++ 中进行“ self ”(this)赋值?

Java:

 public Point(Point p) {
        this(p.x, p.y);
    }

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

会不会只有 this->(constructor of point that takes x, constructor of point that takes y);?

最佳答案

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

Point(const Point &p) : Point(p.x, p.y) { }

请注意,目前还没有编译器完全支持 C++0x;此特定功能尚未在 G++ 中实现。

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

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); }

关于java - C++ this 在构造函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6919006/

相关文章:

java - 如何理解java Socket-Permissions?

java - 实例化类时抛出异常

java - 连接到 Coherence 集群的方法

使用预定义参数引用 C++ 函数

c++ - 在消息发送线程中使用std::condition_variable等待,发生死锁

c++ - 如果不知道数组的大小,LLVM GEP 安全吗?

c++ - C++0x 中的特殊成员函数

c++ - 将工作委托(delegate)给父类(super class)的构造函数

javascript - 替换 Javascript 构造函数的 .prototype 而不是添加它有问题吗?

java - 将java字符串加载到本地存储中