How to check the type name of an object in derived classes?
Posted
by Vincenzo
on Stack Overflow
See other posts from Stack Overflow
or by Vincenzo
Published on 2010-05-05T15:20:34Z
Indexed on
2010/05/05
15:28 UTC
Read the original article
Hit count: 93
c++
This is my code:
class Base { /* something */ };
class Derived : public Base { /* something */ };
vector<Base*> v; // somebody else initializes it, somewhere
int counter = 0;
for (vector<Base*>::iterator i=v.begin(); i!=v.end(); ++i) {
if (typeof(*i) == "Derived") { // this line is NOT correct
counter++;
}
}
cout << "Found " << counter << " derived classes";
One line in the code is NOT correct. How should I write it properly? Many thanks in advance!
© Stack Overflow or respective owner