Thread: Checking Object's class.

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Checking Object's class.

    Code:
    void test(YourClass *obj);
    How to check if the obj is actually YourClass instance, but not YourClass inherited classes instance?

    Should we use RTTI here?

    Thanks in advance. :-D
    Just GET it OFF out my mind!!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why would you want to? It's possible, sure, but usually it's expensive and doesn't need to be done (alternative solutions are better).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    bool operator == (YourClass *obj)
    It seems incorrect if it returns true while obj is a YourClass derived object, isn't it?

    Tried,
    Code:
    dynamic_cast
    but it doesn't work that way.
    Just GET it OFF out my mind!!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken you can use typeid here (naturally you can dynamic_cast derived objects to base).

    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);
    }
    Whatever it is that you are doing.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by audinue View Post
    Code:
    void test(YourClass *obj);
    How to check if the obj is actually YourClass instance, but not YourClass inherited classes instance?

    Should we use RTTI here?

    Thanks in advance. :-D
    Anon's method should work, but why would you need to do that anyway? I'm not being critical, I just can't think of any situation where that'd be useful.

  6. #6
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    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!!

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks anon,..
    Just GET it OFF out my mind!!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    Code:
    bool operator == (YourClass *obj)
    It seems incorrect if it returns true while obj is a YourClass derived object, isn't it?

    Tried,
    Code:
    dynamic_cast
    but it doesn't work that way.
    Are you really trying to define operator == between an object and a pointer? Why?

  9. #9
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    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!!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    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)
    Perhaps I've gone insane, but if you pass something in as a const reference, then don't you slice any of the "derived" stuff away, meaning that what you have is now indistinguishable from a just plain YourClass?

  11. #11
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    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()
    Seems doesn't work like you said.
    Just GET it OFF out my mind!!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    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()
    Seems doesn't work like you said.
    Okay, so passing by const references loses overridden functions but not virtual overridden functions. (There's bound to be better terminology than that.) Neat!

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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).
    It's called the named constructor idiom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. forbid creating copies of objects of child classes
    By sigi in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2008, 09:17 AM
  3. Constructing Base Class Objects
    By Angelina in forum C++ Programming
    Replies: 3
    Last Post: 01-15-2008, 11:28 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM