is there any advantage of using your own type identifier over RTTI?
e.g.
class A { virtual int mytype() = 0; };
class B : public A { int mytype() {return 1;} };
class C : public A { int mytype() {return 2;} };
Could it be faster? Less overhead? Or should one always use RTTI in such a situation?
Don’t assume that RTTI will have more/less overhead than your solution before testing it.
You should try both solutions and measure the performances to get a reliable answer.
I actually asked myself the same question a few years ago and I ended up adding a member variable to “fasten” the type testing, just like you did. Turned out my code was needlessly cluttered with stupid tests while some
dynamic_cast<>would have done the same job (in fact, a better job).I refactored the code to use
dynamic_cast<>since then and I wouldn’t go back.As a foot-note: if your classes are polymorphic, you already “paid” for this anyway, so just go with
dynamic_cast<>.