Overloading casts

This is a discussion on Overloading casts within the C++ Programming forums, part of the General Programming Boards category; I want to make an object that has many faces. By overloading typecasts, the object can show many faces. What ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905

    Overloading casts

    I want to make an object that has many faces. By overloading typecasts, the object can show many faces. What I am wondering, however, is whether there is a way to have C++ assume the casts, rather than me haivng to type them in. Let me show you what I mean.
    Code:
    class CRYPTSTATE
    {
     public:
            CRYPTSTATE(string name_in,int value_in) : name(name_in),value(value_in) {}
            CRYPTSTATE() : value(-1) {}
            operator string() {return name;}
            operator int() {return value;}
            bool operator == (CRYPTSTATE &other) {return (value == static_cast<int>(other));}
            bool operator == (int &other) {return (value == other);}
            bool operator == (string &other) {return (name == static_cast<string>(other));}
            bool operator == (char* other)
                 {
                  string temp(other);
                  return (name == other);
                 }
                 
     private:
            string name;
            int value;
    };
    
    CRYPTSTATE CRYPT_FAIL("CRYPT_FAIL",0); 
    
    string afunction(bool def)
    {
     return (def) ? "This is the default output of afunction()" : CRYPT_FAIL;
    }
    Now, this code will not compile. because
    Code:
    return (def) ? "This is the default output of afunction()" : CRYPT_FAIL;
    CRYPT_FAIL is not a string. If I static_cast<string> it, of course, it works. Is there a way the function can just assume the cast, since it's returning a string?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Usually it will cast implicitly. The ternary conditional operator, however, is a special case where user-defined implicit casts simply won't work. Sorry.

    On the other hand, a design with so many cast operators is not exactly ideal anyway. Casts, because they are implicit, are part of what I call "hidden code execution" and thus make a program harder to understand.
    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

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Gotchya.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I using casts properly?
    By Dondrei in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2009, 12:51 AM
  2. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  3. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  4. operator overloading for streams
    By rozner in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2006, 08:00 PM
  5. Operator= overloading from another type
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2006, 08:15 PM

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