overloading cast operator

This is a discussion on overloading cast operator within the C++ Programming forums, part of the General Programming Boards category; I want to overload the casting operator so that I can take anouther class and return a new class from ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    overloading cast operator

    I want to overload the casting operator so that I can take anouther class and return a new class from that data.

    Code:
    operator ::NewtonBody* ();
    This code almost does what I want. But this will convert the current class into anouther type. I want the oposite.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sorry, but the following operators cannot be overloaded:
    Code:
    . (member access)
    -> (pointer member access)
    (cast) (C-style cast)
    ?: (ternary conditional operator)
    There may be others, I just can't think of them at the moment.

    [edit] Whoops. http://www.parashift.com/c++-faq-lit....html#faq-13.5
    Most [operators] can be overloaded. The only C operators that can't be are . and ?: (and sizeof, which is technically an operator). C++ adds a few of its own operators, most of which can be overloaded except :: and .*.
    [/edit]

    [edit=2] Perhaps like this? http://charm.cs.uiuc.edu/users/olawl...erload_all.cpp (No, I did not write that code. It's the first hit when googling for "overload cast operator C++".) [/edit]
    Last edited by dwks; 03-31-2007 at 07:10 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Ok ya that helped now. I got it to do what I want, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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