CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Location
    Germany
    Posts
    15

    Find out what class an object belongs to after casting

    What can I do in order to get this running without using wrapper classes?

    class MyClass
    {
    public:
    int x;
    int y;
    };

    void print(void* x)
    {
    // Here I need to
    // know what type x is of
    // (int or double or Myclass, or anything...)
    }

    void main(void)
    {
    int* x1 = new int(3);

    MyClass* x2 = new MyClass();

    x2->x = 5;
    x2->y = 5;

    print(x1);
    print(x2);

    delete x1;
    delete x2;
    }

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Nope, can't be done, at least not in any sane way.

    A much better approach would be to add a print() method to MyClass, or even better and operator<<.

    Jeff

  3. #3
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I agree with jfaust. Let the work be done at compile type and
    overload operator<<. The compiler WILL call the proper
    operator<< for your class; it's called function overloading.

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747

    additionally

    I don't suggest that its appropriate for your case (where your class can do the work), but there are methods available to check class type at runtime. You could use the type_info class returned by taking the typeid operator on a class, and in fact you can compare them, like
    Code:
    if (typeid(SomeClass) == typeid(int))
    {
      ...
    }
    but its only there if you cannot find a way to do it using other methods...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  5. #5
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    I have to agree to jfaust and PaulWendt:

    the only clean solution should be an overloading of the needed types:
    Code:
    void print(int)
    void print(double)
    ...
    and for classes (template-design):
    Code:
    template <class TYPE>
    void print(TYPE& t)
    {
     t.print(); // assuming TYPE-class has a func called print()
    }
    or base-class-design (if prefering heredity):
    Code:
    void print(CBase& rBase)
    {
     rBase.print(); // assuming rBase-class has a func called print()
    }
    Mikey
    Last edited by Mikey; November 27th, 2002 at 06:34 PM.

  6. #6
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166

    expanding my last mail...

    ... but You can also override the ()-operator:
    Code:
    CMyClass operator()(int i)
    {
     CMyClass oTemp; 
    put i in oTemp to use it later in print()
    return oTemp;
    }
    
    CMyClass operator()(double dbl)
    {
     CMyClass oTemp; 
     put dbl in oTemp to use it later in print()
     return oTemp;
    }
    using:
    Code:
    int i = 0xffffffff;
    double dbl = 0xffffffffffffffff;
    CMyClass oMyClass;
    print<CMyClass>(oMyClass); // using oMyClass.print()
    print<CMyClass>(oMyClass(i)); // first call (int)-op and then oTemp.print()
    print<CMyClass>(oMyClass(dbl)); // first call (double)-op and then oTemp.print()
    Surely You can play a bit with that to fit it to Your request!

    Mikey
    Last edited by Mikey; November 28th, 2002 at 04:17 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured