can int operator is overloadable?

Hi,

I have saw the small snippets of code where they overload the "int" operator ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream> 
using namespace std; 

class CB{ 
  private: 
    int m_nState; 
  public: 
    CB(int nState = 0): m_nState(nState){cout << 1;} 
    CB(const CB& obj){ m_nState = obj.GetState();} 
    ~CB(){} 
   operator int(){return m_nState;} 
	
    int GetState() const {return m_nState;} 
    CB& operator= (const CB& obj){ 
      m_nState = obj.GetState(); 
      return *this; 
    } 
}; 

int main(int argc, _TCHAR* argv[])
{
  CB obj1 = 2; 
  CB obj2; 
 obj2 = obj1 + 3; 
 
}


can any one explain why int is overloaded and my doubt how we consider int as operator ??
and more thing when i trying to overload the float in the same code compiler is showing this error "more than one conversion function from "CB" to a built-in type" ..
Last edited on
The operator int() allows implicit and explicit casting to ints. This means that when the CB functionality cannot be applied, it tries if it works for an int (implicit) and you can directly cast a CB object to an int value (by using int(x), (int)x, dynamic_cast<int>(x) or static_cast<int>(x)).
operator int means that you can convert an object of your class to an integer. you can have such operators for any type. They can often lead to errors though. They occur when compiler can't decide which conversion to use.
too slow..
Last edited on
thanks 2 all 4r the reply ..

why system is generating the error message when i add the overloading for float also ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream> 
using namespace std; 

class CB{ 
  private: 
    int m_nState; 
  public: 
    CB(int nState = 0): m_nState(nState){cout << 1;} 
    CB(const CB& obj){ m_nState = obj.GetState();} 
    ~CB(){} 
   operator int(){return m_nState;} 
   operator float(){return m_nState;} 
	
    int GetState() const {return m_nState;} 
    CB& operator= (const CB& obj){ 
      m_nState = obj.GetState(); 
      return *this; 
    } 
}; 

int main(int argc, char* argv[])
{
  CB obj1 = 2; 
  CB obj2; 
 obj2 = obj1 + 3; 
 obj2 = obj1 + 5.0 
 
}


Because in C++, adding variables of type char and float and int and double to each other is implicit and legal.

So for the compiler to do do this:
obj2 = obj1 + 3;
the compiler can use either operator float() or operator int() so
it complains about the ambiguity.

the same goes for this statement:
obj2 = obj1 + 5.0
...which drives me nuts, because the compiler should be smart enough to know that if you write "5.0" that is a floating-point (because it has a big old decimal point in it), and that "3" is an integer (because it does not have a honkin' decimal point).

If the operation of the routines is so different between int and float then there is a problem with the code, not the compiler.

I think that the the compiler should first look for an exact match, then matches in the same type class (float != int)!!!, then try upcasting.

Too bad C++ is saddled with those horrible C typing semantics.
Topic archived. No new replies allowed.