Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

binary operator overloading; implicit type conversion

$
0
0
Code:

    class my_bool {
      private:
        bool value;
      public:
        my_bool(bool value) : value(value) {}
        explicit operator bool() { return value };
   
        friend my_bool operator==(const my_bool & instance_1, const my_bool & instance_2);
        friend my_bool operator&&(const my_bool & instance_1, const my_bool & instance_2);
   
    };
   
    void main(){
      my_bool a = true;
      bool b = false;
   
      if(a == b){
        // do something
      }
     
      if(a && b){
        // do something
      }
    }

So, operator== causes no ambiguities here and operator&& does. Why is that? Is there a way I can solve it without writing down multiple (bool, my_bool), (my_bool, bool), (my_bool, my_bool) operator&& overloads?

Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images