可能重复:
How do you use the non-default constructor for a member?C++:如何声明私有成员对象
我当前的代码:
class ImagePoint {
private:
int row;
int col;
public:
ImagePoint(int row, int col){
this->row = row;
this->col = col;
}
int get_row(){
return this->row;
}
int get_col(){
return this->col;
}
};
而且我想这样做:
class TrainingDataPoint{
private:
ImagePoint point;
public:
TrainingDataPoint(ImagePoint image_point){
this->point = image_point;
}
};
但是这不会编译,因为行ImagePoint point;
要求ImagePoint
类有一个空的构造函数。 (从我读)的替代说我应该使用指针:
class TrainingDataPoint{
private:
ImagePoint * point;
public:
TrainingDataPoint(ImagePoint image_point){
this->point = &image_point;
}
};
然而,一旦构造函数运行完毕后会这个指针指向清理对象?如果是这样,我是否必须复制image_point
?这将需要一个复制构造函数吗?
这将使image_point的副本,并将其存储点? – Aly
@是的。 [填写] –
谢谢,当SO让我时,我会在10分钟内接受答案:)。 – Aly