C++ Determine the type of a polymorphic object at runtime
<p>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:</p>

<pre><code>class A{
};
class B: public A{
    public:
        void foo(){
            printf("B::foo()\n");
        }
};
</code></pre>

<p>Later, I will have a bunch of B objects that are essentially stored as such:</p>

<pre><code>std::vector&lt;A*&gt; v;
v.push_back(new B());
</code></pre>

<p>And I will need to call certain overloaded methods defined as:</p>

<pre><code>void bar(B* b){
    b-&gt;foo();
}
</code></pre>

<p>After passing in objects that are stored in <code>v</code>. The problem that I am having is that in my actual use-case, I don't know the type of <code>B</code> at compile-time, so I can't just call <code>bar</code> by saying <code>bar((B*)v.get(0));</code></p>

<p>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 <code>bar</code>.</p>

<p>The solution I have tried so far was to use <code>decltype</code>, but it didn't work for me because it just returns the static type of the value passed in, not the type at runtime.</p>

<p>Also, I do not want to use third party libraries for this project, since I would like to make it as small as possible.</p>

<p>Thank you for your help.</p>

<p>Edit:</p>

<p>I don't think I was clear enough in what I meant when I was describing my problem. In my actual use case (which is a bit long to try posting here, though I can post parts of it if necessary), I am trying to write a library in which there is a base class (represented here as <code>A</code>) which can be extended by the user into their own custom classes (represented here as <code>B</code>). In my example, <code>B::foo()</code> is simply supposed to represent how each subclass of <code>A</code> can have its own data members that are handled later by some method (represented in my example as <code>bar</code>). This is also the reason why <code>A</code> cannot simply have some virtual <code>foo</code> method.</p>

<p>The main issue I'm having is, since <code>B</code> is supposed to be user-defined, I don't know what it is at compile-time, but I do at link-time (since linking comes later). That is why the use of <code>dynamic_cast</code> (as suggested by Sam and Remy) won't work, because from what I understand of  it, it requires that I know every possibility of what <code>B</code> could be, which I don't. Although it did look very close to what could work for me. If there was some way to get all possible subclasses (say, with preprocessor macros or templates), then I think this might be able to work.</p>

<p>I hope I've explained it better this time. Thank you again for your help.</p>

<p>Edit 2: Another clarifying point: In the actual project I am working on, I only want the user to be required to write their own <code>B</code> classes and overload <code>bar</code> to work with their custom classes. However, I don't want the user to be required to call <code>bar</code>. Rather, I would like to have <code>bar</code> be called from a base class (that I define). That is the main reason for even attempting to do this the way I am. That, and to see if I can.</p>

<p>Hope that clears everything up. Thank you again.</p>