Is it possible to assign an int object?

I have a CCounter class that contains an integer value protected by mutex. I have defined several statements, such as post / pre inc / dec, that return an integer so that I can:

CCounter c(10); int i = c++; 

but what should I do with a simple assignment like i = c ? I tried to define the friend = operator, but it gives me

operator=(int&, const CCounter&)' must be a nonstatic member function

mistake. Please advise. Thanks.

+4
c ++ operator-overloading
source share
8 answers

You need to define a casting statement that executes from CCounter to int. Add this member to your class:

 operator int() const { return ...; } 
+16
source share

As you found out, the assignment operator must be a member function of the class. Since ints are not classes, you cannot write operator = () for them. An alternative, as others have pointed out, is to write a function that converts to int. I would strongly suggest that you write a named function such as ToInt () to do this, instead of using the conversion operator, which can be the source of non-obvious errors.

+7
source share

You need to define operator int() to enable the conversion of your class to int. For example:

 class CCounter { public: CCounter(int val) : m_val(val) { } operator int() const { return m_val; } private: int m_val; }; int main(int argc,char *argv[]) { CCounter c(10); int n = c; std::cout<<n<<"\n"; return 0; } 
+1
source share

G'day

Shouldn't you define an access function instead if you just “get” the current counter value?

Something like:

 int GetCounter(); 

Something else hides the intention of what you are trying to do. IMHO Natch! (-:

NTN

amuses

+1
source share

As said, use the int () operator. Here's the code snippet:

 #include <iostream> class CCounter { public: CCounter(int i = 0) : _count(i) {} operator int() { return _count; } private: int _count; }; int main() { CCounter counter(4); int c = counter; std::cout << "Counter = " << c << std::endl; return 0; } 
+1
source share

You said:

"I have defined several operators, such as post / pre inc / dec, that return an integer."

Now that the other answers have provided you with a general way of converting an object to an integer, I would recommend that you modify these other operators so that they behave as normally expected.

For example, pre increment usually returns a reference to the object itself, and post increment usually returns a temporary copy of the original object (before the increment).

 CCounter& operator++() { ++m_val; return *this; } CCounter operator++(int) { CCounter tmp(*this); ++m_val; return tmp; } 
+1
source share

Although you have been given a valid solution , I would also consider simply creating a normal function that returns an int , such as int GetValue() const , to improve readability and ease of maintenance. Of course, this is very subjective.

0
source share
 #include<iostream> using namespace std; class CA { public: int a; CA(int x):a(x) { } operator int() const { return a; } void operator ()() { } }; void main(){ CA obj = 100; int k = obj; obj(); } 
0
source share

All Articles