Thread: Can my class know/handle casting?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Can my class know/handle casting?

    With assignment and copying you can implement an operator to handle those operations ina custom manner. Is it possible to do the same for casting of pointers? I know that in casting objects I can simply make a constructor that takes the Base type and it'll handle it, but what about pointers?

    Code:
    class A
    {
         virtual void foo() {}
    };
    
    class B : public A
    {
    };
    
    int main()
    {
         A* a;
         B* b;
    
         //Is there a way to write a function in A that will be called when i do either of these?
         b = ( B* ) a;
         b = dynamic_cast<B*> a;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just have class A have a virtual copy() function and the equal operator. Then the equal operator will always call the copy() for the most recently defined version of copy().

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by master5001 View Post
    Why not just have class A have a virtual copy() function and the equal operator. Then the equal operator will always call the copy() for the most recently defined version of copy().
    The only problem with that is it doesn't handle this case:

    Code:
    B* b =  new B();
    
    ( ( A* ) b )->SomeMethod();

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you need that case then I would imagine something is seriously wrong with your design. As usual, what are you trying to do?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You can overload a conversion. You can not overload a cast.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes, of course. I misunderstood what you wanted. I betcha can't name a language that does work like that.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    it will work if

    A *a = new B();

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually, you can overload a cast. But you can't overload anything for types you didn't define, and pointers all count as built-in types.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by m37h0d View Post
    it will work if

    A *a = new B();

    this apparently is not true if B inherits virtually from A

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    this apparently is not true if B inherits virtually from A
    No, it is still true, as long as public inheritance is used. Of course, in the above code foo() is a private member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    Actually, you can overload a cast. But you can't overload anything for types you didn't define, and pointers all count as built-in types.
    You can overload dynamic_cast<>, static_cast<>, or const_cast<>? That's what I mean by "cast."

    Expressions like (Type1)obj2 are often said to be "casting" obj2 to Type1, but I tend to call it a conversion, not a cast.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Expressions like (Type1)obj2 are often said to be "casting" obj2 to Type1, but I tend to call it a conversion, not a cast.
    Yeah, the C++ Standard calls them conversion functions too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I meant you can overload the conversion abilities, indeed. You can't overload the four cast operators, which, by the way, is a pity.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM