I am trying to work on a project that will require me to determine a polymorphic object's type at runtime, so that I can cast it. An example of what I mean:
class A{
};
class B: public A{
public:
void foo(){
printf("B::foo()\n");
}
};
Later, I will have a bunch of B objects that are essentially stored as such:
std::vector<A*> v;
v.push_back(new B());
And I will need to call certain overloaded methods defined as:
void bar(B* b){
b->foo();
}
After passing in objects that are stored in v
. The problem that I am having is that in my actual use-case, I don't know the type of B
at compile-time, so I can't just call bar
by saying bar((B*)v.get(0));
The solution I have been thinking I might need is to somehow determine the type that each object is at runtime, so that I can cast it before passing it to bar
.
The solution I have tried so far was to use decltype
, but it didn't work for me because it just returns the static type of the value passed in, not the type at runtime.
Also, I do not want to use third party libraries for this project, since I would like to make it as small as possible.
Thank you for your help.
Aucun commentaire:
Enregistrer un commentaire