C++ - 使用另一个 class 的构造函数实例化一个对象

C++ - Instantiating an object with a constructor of another class

我有 classes 称为 Point 和 Vector。我希望能够从 Point 对象初始化 Vector 对象,该对象是通过调用其构造函数创建的。但是我收到以下错误:对象被 初始化为非 class 类型‘Vector(Point (*)())’

// in Point.h

class Point{
    double x,y,z;
public:
    Point();
    Point(double, double, double);
};

// in Vector.h

class Vector{
public:
    Vector();
    Vector(double, double, double);
    Vector(const Point&);
    f();
};

// in  main.cpp
int main(){
    Vector v(Point(0,0,0)); // OK
    Vector w(Point()); // 'error'
    v.f(); // OK
    w.f(); // error
}

为什么它不使用在 Point() 的默认构造函数中创建的 Point 对象来初始化对象 'w'?它能够为对象 'v'.

做类似的事情

我尝试使用

 Vector w(new Point());

但这也给出了编译错误,我想我明白了。

实际中最令人烦恼的解析:

Vector w(Point()) 被解析为函数声明:

改用{}

Vector w{Point{}};