Can my class know/handle casting?

This is a discussion on Can my class know/handle casting? within the C++ Programming forums, part of the General Programming Boards category; With assignment and copying you can implement an operator to handle those operations ina custom manner. Is it possible to ...

  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,340
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    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
    839
    it will work if

    A *a = new B();

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    839
    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
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    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
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21