How to check if the obj is actually YourClass instance, but not YourClass inherited classes instance?Code:void test(YourClass *obj);
Should we use RTTI here?
Thanks in advance. :-D
How to check if the obj is actually YourClass instance, but not YourClass inherited classes instance?Code:void test(YourClass *obj);
Should we use RTTI here?
Thanks in advance. :-D
Just GET it OFF out my mind!!
It seems incorrect if it returns true while obj is a YourClass derived object, isn't it?Code:bool operator == (YourClass *obj)
Tried,
but it doesn't work that way.Code:dynamic_cast
Just GET it OFF out my mind!!
If I'm not mistaken you can use typeid here (naturally you can dynamic_cast derived objects to base).
Whatever it is that you are doing.Code:#include <iostream> #include <typeinfo> struct Base { virtual ~Base() {} }; struct Derived: Base {}; void test(Base* p) { std::cout << (typeid(*p) == typeid(Base)) << '\n'; } int main() { Base b; Derived d; test(&b); test(&d); }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
To make our program reliable and correct.
Please look at reply #3.
This is insane, in contrast to efficiency I think.. Whatever..
Just GET it OFF out my mind!!
Thanks anon,..
Just GET it OFF out my mind!!
Sorry, I'm just lazy to type: const YourClass &obj on my phone keypad.
Usually I just use my global macro:
Code:CR(YourClass, obj)
Just GET it OFF out my mind!!
Seems doesn't work like you said.Code:class A { public: virtual void a() const { cout << "A::a()" << endl; } }; class B : public virtual A { public: virtual void a() const { cout << "B::a()" << endl; } }; void test(const A &a) {a.a();} test(A()); //A::a() test(B()); //B::a()
Just GET it OFF out my mind!!
If you pass by value only what the objects have in common will be copied.
Either way you should document that your code doesn't address the slicing problem and it is invalid to pass Derived objects to test, or whatever.
I think you are doing something wrong in your code if a derived class is generally valid, but can't be used where the base-class can be used. You either haven't made something virtual that should be virtual, or you are doing something else quite wrong.
Or you should forbid inheritance (which I think there is a common pattern for how you do that, but I couldn't be bothered to searching long enough to find it - I'm sure someone else is able to show it, if need be).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
It's called the named constructor idiom.Or you should forbid inheritance (which I think there is a common pattern for how you do that, but I couldn't be bothered to searching long enough to find it - I'm sure someone else is able to show it, if need be).