【发布时间】:2015-07-26 22:08:13
【问题描述】:
我有一个需要初始化的类:
//Parent.h
class Parent {
public:
Parent(Image image);
private:
const Image parentImage;
}
//Parent.cpp
Parent::Parent(Image image) : parentImage(image) {}
//Child.h
#import "Parent.h"
class Child : public Parent {
public:
Child(int c);
private:
Camera childCamera;
}
//Child.cpp
Child::Child(int c) : Parent(this->childCamera.getImage()), childCamera(c) {}
Camera 需要先初始化,然后才能从中检索图像。 Parent 存储来自相机的图像,即const,子级存储Camera。如何创建Child?我无法更改Parent,因为还有其他子类以其他方式初始化Parent。 Child可以改,但是Camera不能复制,Child确实需要存储Camera。
编辑:如果这是最干净的解决方案,我可以将构造函数添加到 Parent。
【问题讨论】:
-
什么是
this->childDataSource.getImage()?您的代码显示childCamera和Parent之间没有依赖关系 -
对不起,应该是
this->childCamera.getImage()。真实情况有更多的成员等等,我在简化的时候出错了。
标签: c++ inheritance initialization