将子类存储在超类向量中
Storing subclasses in superclass vector
我正在尝试为偶发噪声库构建一个序列器,用户可以在其中生成一个描述各种类型噪声层的XML文件,并获得结果。我很难理解继承在C++中是如何工作的。
不幸的是,并不是所有子类的方法(在本例中为CImplicitSphere)都是在超类(CImplicitModuleBase。这就是我目前所掌握的:
std::vector<anl::CImplicitModuleBase *> layers;
anl::CImplicitSphere tmp;
tmp.setCenter(0.0,0.0,0.0,0.0,0.0,0.0);
layers.push_back(&tmp);
value = layers.back()->get(0.0,0.0,0.0);
但我遇到了一个"纯虚拟方法"错误——这就是我陷入困境的地方——我需要键入cast吗?对此有一些讨论,但似乎表明这是个坏主意。
有没有更好的方法来构建我的代码,这样我就可以直接在层向量中初始化CImplicitSphere,并在进行时指向子类方法?
完整来源:
void ScalarTerrain::setupAccidentalNoise() {
printf("Making an array of noise functionsn");
std::vector<anl::CImplicitModuleBase *> layers;
try {
printf("Getting data for next layern");
pugi::xml_node layer = terrainData.child("layer");
if(strcmp(layer.attribute("type").value(), "sphere") == 0) {
printf("Layer is a sphere building a temp layern");
anl::CImplicitSphere tmp;
tmp.setCenter(
layer.child("center").attribute("x").as_double(),
layer.child("center").attribute("y").as_double(),
layer.child("center").attribute("z").as_double(),
layer.child("center").attribute("u").as_double(),
layer.child("center").attribute("v").as_double(),
layer.child("center").attribute("w").as_double()
);
layers.push_back(&tmp);
}
else {
printf("Layer type not foundn");
}
} catch (char * exception) {
printf("Exception raised: %sn", exception);
}
try {
for(int z = 0; z < z_chunk; z++) {
for(int y = 0; y < y_chunk; y++) {
for(int x = 0; x < x_chunk; x++) {
value = layers.back()->get(
(double) x/x_chunk * 2,
(double) y/y_chunk * 2,
(double) z/z_chunk * 2
);
tc.values[x][y][z] = value;
if(value < -0.5) tc.materials[x][y][z] = 0;
else if (value < 0) tc.materials[x][y][z] = 1;
else if (value < 0.5) tc.materials[x][y][z] = 2;
else tc.materials[x][y][z] = 3;
}
}
}
} catch (char * exception) {
printf("Exception raised: %sn", exception);
}
}
这似乎有效,而且并不难看:
void ScalarTerrain::setupAccidentalNoise() {
printf("Making an array of noise functionsn");
std::vector<anl::CImplicitModuleBase *> layers;
anl::CImplicitModuleBase * tmp;
//anl::CImplicitSphere thisLayer;
try {
printf("Getting data for next layern");
pugi::xml_node layer = terrainData.child("layer");
if(strcmp(layer.attribute("type").value(), "sphere") == 0) {
printf("Layer is a sphere building a temp layern");
tmp = new anl::CImplicitSphere();
dynamic_cast<anl::CImplicitSphere*>(tmp)->setCenter(
layer.child("center").attribute("x").as_double(),
layer.child("center").attribute("y").as_double(),
layer.child("center").attribute("z").as_double(),
layer.child("center").attribute("u").as_double(),
layer.child("center").attribute("v").as_double(),
layer.child("center").attribute("w").as_double()
);
layers.push_back(tmp);
}
else {
printf("Layer type not foundn");
}
} catch (char * exception) {
printf("Exception raised: %sn", exception);
}
try {
printf("Iterating through blocksn");
for(int z = 0; z < z_chunk; z++) {
for(int y = 0; y < y_chunk; y++) {
for(int x = 0; x < x_chunk; x++) {
printf("getting block value at %d %d %dn",x,y,z);
value = layers.back()->get(
(double) x/x_chunk * 2,
(double) y/y_chunk * 2,
(double) z/z_chunk * 2
);
tc.values[x][y][z] = value;
if(value < -0.5) tc.materials[x][y][z] = 0;
else if (value < 0) tc.materials[x][y][z] = 1;
else if (value < 0.5) tc.materials[x][y][z] = 2;
else tc.materials[x][y][z] = 3;
}
}
}
} catch (char * exception) {
printf("Exception raised: %sn", exception);
}
}
这是"正确"的做事方式吗?还是我偏离了目标?
相关文章:
- 模板转换为超类
- 将向量作为类>(值)<向量启动和向量<类>[值]有什么区别
- 如何从静态向量访问类元素?
- 我的超类中的模板问题与结构定义
- 在具有向量的类构造函数中进行析构函数调用
- 创建子类的多个实例,其中只有一个超类实例
- 给定一个指向对象的超类指针,如何获取对象的大小?
- 重写另一个方法 [C++] 使用的超类回调函数
- 用初始化列表和超类构造函数声明子类构造函数的正确方式
- 我们可以在没有新实例化的情况下声明一个抽象方法来返回抽象超类中的子类对象吗
- C++为API中定义的结构创建超类
- 使用派生类C++方法的超类
- 确保指向超类的指针指向c++中某个子类的对象
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 如何将项目放入具有模板化超类类型的向量中
- 将基类的向量传递给接受超类向量的函数
- 制作可以存储子类对象的超类类型的向量
- 将子类存储在超类向量中
- 如何迭代定义为超类的向量,但调用子类方法
- C++:在超类向量中查找类的实例