<分区>
Possible Duplicate:
How do you use the non-default constructor for a member?
我有当前代码:
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
?这需要复制构造函数吗?