Thread: overloading cast operator

  1. #1
    Registered User
    Join Date
    Aug 2004

    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.

    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
    Sorry, but the following operators cannot be overloaded:
    . (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.
    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=2] Perhaps like this? (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.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2004
    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, 02: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, 08: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