如何从包含基类指针的容器中调用派生类函数(基于其类型)?

How to call derived class function (based on its type) from a container which contain pointer of base class?

本文关键字:类函数 派生 于其 类型 调用 包含 基类 指针      更新时间:2023-10-16

我从基类继承来创建两个不同的派生类(Derived1和Derived2(,然后将它们放入向量中。假设我想根据类的类型调用派生类的函数。

伪代码:

if holder[1] stored Derived1 then I want to call GetZ() 
else if holder[1] stored Derived2 then I want to GetFlag(). 

一次尝试:

#include <iostream>
#include <memory>
#include <vector>
class Base {
public:
Base(int x, int y) : x_(x), y_(y) {}
int GetX() { return x_; }
int GetY() { return y_; }
private:
int x_;
int y_;
};
class Derived1 : public Base {
public:
Derived1(int x, int y, int z) : Base(x, y), z_(z) {}
int GetZ() { return z_; }
private:
int z_;
};
class Derived2 : public Base {
public:
Derived2(int x, int y, bool flag) : Base(x, y), flag_(flag) {}
bool GetFlag() { return flag_; }
private:
bool flag_;
};
std::vector<std::shared_ptr<Base>> holder;
void print();
int main() {
holder.push_back(std::make_shared<Derived1>(3, 4, 5));
holder.push_back(std::make_shared<Derived2>(6, 7, true));
print();
}
void print(){
for(auto& it : holder){
// call this if "it" is Derived2
// else call it->GetX()
// currently this gives compilation error 
// because of object slicing
std::cout << it->GetFlag() << std::endl;
}
}
for(auto& it : holder){
if (auto* D1 = dynamic_cast<Derived1*>(it->get())) {
std::cout << D1->GetZ();
} else if (auto* D2 = dynamic_cast<Derived2*>(it->get())) {
std::cout << D2->GetFlag();
}
std::cout << std::endl;
}

动态强制转换通常是代码异味,证明您的界面Base缺少功能。 动态转换后,界面将从Base状态变为整个类型层次结构的布局和内容。

相反,请添加:

virtual boost::optional<int> GetZ() { return {}; }
virtual boost::optional<bool> GetFlag() { return {}; }

Base,并在派生中覆盖。

for(auto& it : holder){
if (auto Z = it->GetZ()) {
std::cout << *Z;
} else if (auto flag = it->GetFlag())
std::cout << *flag;
}
std::cout << std::endl;
}

现在我们不再关心我们用来实现ZFlag的特定派生类型。

从这个 SO 答案中,有一个指向参考 std::可选实现的链接,该实现使用 boost 软件许可证并且是一个头文件。

检查好的dynamic_cast技巧 - 如果它是可铸造的,它是正确的类型。

无论如何,我建议不要使用这种设计模式(在运行时检查类型并据此做出决定(,而是将逻辑放入派生类中;有一个通用方法可以做一件事或另一件事:

class Base {
public:
Base(int x, int y) : x_(x), y_(y) {}
int GetX() { return x_; }
int GetY() { return y_; }
virtual int do_the_right_thing();
private:
int x_;
int y_;
};
class Derived1 : public Base {
public:
Derived1(int x, int y, int z) : Base(x, y), z_(z) {}
int GetZ() { return z_; }
virtual int do_the_right_thing() { return GetZ() };
private:
int z_;
};
class Derived2 : public Base {
public:
Derived2(int x, int y, bool flag) : Base(x, y), flag_(flag) {}
bool GetFlag() { return flag_; }
virtual int do_the_right_thing() { return GetFlag() };
private:
bool flag_;
};

void print(){
for(auto& it : holder){
// call this if "it" is Derived2
// else call it->GetX()
// currently this gives compilation error 
// because of object slicing
std::cout << it->do_the_right_thing() << std::endl;
}
}

STL风格的处理方式是模板和类型特征 - 但我个人认为这很麻烦。