This question came up in a class I was attending. Consider:
class foo{virtual void foo_fun() = 0;};
class bar{virtual void bar_fun() = 0;};
template<typename T>
bool is_a_bar(T* f) {
// I don't know baz.
// Can I somehow know if f implements bar, T being any other type?
}
class baz : public foo, public bar {
void foo_fun() override {}
void bar_fun() override {}
};
#include <iostream>
int main() {
foo* a{new baz};
if (is_a_bar(a)) // Will call is_a_bar(foo*)
std::cout << "a is also a bar\n";
}
Is it possible to know if an arbitrary object, deriving from foo or bar, also derives from the other...without knowing what the actual object is?
(Assume that I can't change foo or bar to provide this information.)
Aucun commentaire:
Enregistrer un commentaire