我正在创建一个具有子类的抽象几何类。但是,我想要类(class) RightCircularCone
也有自己的私有(private)变量来定义它的顶点坐标,这样抽象类对于 Sphere
类型的对象来说内存大小就不是不必要的大了。不需要存储顶点变量。
但是,我似乎无法访问 RightCircularCone
的函数和变量。当我从使用智能指针的容器中加载它们时,因为它一直被定义为其父类 Shape
.任何人都可以看到出了什么问题吗?!欣赏它!
/* shapes.hpp */
class Shape{
public:
unsigned int color;
float radius;
float x,y,z;
public:
void SetSpatial(float radius, float x, float y, float z);
unsigned int GetColor(void);
void SetColor(unsigned int color);
virtual bool PointInside(const std::array<double,3> &point)=0;
};
class RightCircularCone : public Shape{
private:
float Ax,Ay,Az;
public:
bool PointInside(const std::array<double,3> &point);
void SetApex(float x, float y, float z);
void PrintApex();
};
class Sphere : public Shape{
public:
bool PointInside(const std::array<double,3> &point);
};
上面定义的类在另一个 .cpp 文件中使用,其中定义了类的方法:
#include "../../include/part.hpp" /* includes shapes.hpp in turn */
void Part::ReadPartFile(std::string partfile){
try{
std::ifstream dataFile;
dataFile.open(partfile);
//do checks, error badbits etc...
std::string word;
unsigned int counter=0;
while(!dataFile.eof()){
dataFile >> word;
if(word == "sphere"){
auto newSphere = std::make_shared<Sphere>();
// load variables into objects from file by checking each word by using setColor and setSpatial
shapeList[counter++] = newSphere;
} else if(word == "rccone"){
auto newRccone = std::make_shared<RightCircularCone>();
// load variables into objects from file by checking each word by using setColor and setSpatial and setApex
shapeList[counter++] = newRccone;
}
}
dataFile.close();
} catch(std::ifstream::failure e) {
//do exception handling here if necessary
}
现在,当我在 map 上使用迭代器时 std::map<unsigned int, std::shared_ptr<Shape> > shapeList;
如 part.cpp
中所定义我永远无法访问子类的方法Sphere
和 RightCircularCone
因为 map 返回类型 Shape
,虽然我用的是智能指针!!!
任何人都知道为什么和潜在的解决方法(或设置类(class)的更简洁的方法)?
谢谢!
//编辑:
这是我得到的错误:
error: no member named 'PrintApex' in 'Shape'
iterator->second->PrintApex();
最佳答案
想一想。您创建一个 shape 的 shared_ptr vector 。就 vector 而言,您在其中存储 Shape 实例,而不是 Sphere 或其他任何东西。
现在,您碰巧用 Sphere 初始化了 Shape 实例,并将该 Sphere 的 ptr 存储到 vector 中。下一项是 RightCircularCone,同样存储为 Shape。
您访问第一个元素,就此时编译器所关心的而言,您只有一个 Shape。它无法推断出实际类型,因为这发生在运行时。因此,就编译器而言,无论 Shape 类包含什么,您都有一个 Shape 实例。
现在,您需要以某种方式通知编译器您想要使用的类型,以便它可以找到您想要访问的方法。为此,您使用 dynamic_cast 来指定这是一个 Sphere 的意图,并且您应该有权访问 Sphere 成员。
有关 dynamic_cast 的更多详细信息,请在此处 http://en.cppreference.com/w/cpp/language/dynamic_cast
编辑。关于设计的注释。
原则上,您希望将尽可能多的工作卸载给编译器。如果有编译器可以做的事情,就让他去做。因此,如果您想为 Shape 的每个子类做一些不同的事情,而不是检查 Shape 类型的字符串文字,您可以使用作用于特定类型的虚拟专用函数。例如
virtual void printMe(Sphere & sphere) {
cout << "This is a sphere with radious x" << endl;
}
virtual void printMe(RightCircularCone & cone) {
cout << "This is a cone" << endl;
}
//and you use like
for (auto & shape: shapes) { printMe(shape); }
//and the correct functions are resolved automagically
它可能看起来有点多,但从长远来看,它实际上更简单,因为您将功能的选择工作交给了其他人。
关于c++ - 抽象类继承和智能指针容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28077136/