Re: Uniquely identifying types at runtime
On Monday, May 19, 2014 7:48:03 PM UTC+1, fmatthew5876 wrote:
As long as each type (polymorphic or not) has a unique typeid()
it should solve my problem.
I will be using typeid() on classes which may not be using inheritance
at all.
Actually, the case where typeid is used with types that do not belong to
a hierarchy is the simple case where it certainly works as you want.
More generally, if I understand you correctly, your expectations will
also be met for types that belong to a non polymorphic classes.
You might have trouble with polymorphic classes. For instance,
#include <cassert>
#include <typeinfo>
struct base {
virtual ~base() {}
};
struct derived : base {
};
int main() {
derived d;
base b;
base& r = d;
assert(typeid(b) != typeid(d)); // passes
assert(typeid(r) == typeid(b)); // fails but will pass if is ~base()
// is not virtual.
assert(typeid(r) == typeid(base)); // ditto
}
When you apply typeid to an object of polymorphic type, the typeinfo
returned is the one for the most derived type. In the example above
(with ~base() being virtual) typeid(r) == typeid(derived) but if you
make ~base() non virtual then typeid(r) == typeid(base).
HTH,
Cassio.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]