Thread: typecast overloading

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    typecast overloading

    Hopefully most of us know about things like static_cast... I'm not referring to those though..

    say you have a class called "any"

    And you want to be able to do this:

    (int)someany;

    or

    int(someany);

    is there a way to write this, or will C++ not let you create them for the built in types?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Why would you want to cast a class to an int? If you want an integer representation of a class, just make a member function or operator for the class that gives you an integer. I don't think that casting a class to an int is possible, not sure though. You can cast an int* to a class* with reinterpret_cast<>

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    My thinking is then you could pass an object to a function that wouldn't normally expect it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Operator= overloading from another type
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2006, 08:15 PM
  4. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  5. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM