views:

249

answers:

7

I have a CCounter class which holds and integer value protected by mutex. I've defined several operators like post/pre inc/dec returning an integer so I can do:

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

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

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

error. Please, advise. Thanks.

+12  A: 

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

operator int() const {
  return ...;
}
Martin Liversage
A: 

You need to define operator int() to allow the conversion of your class to an 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;
}
Naveen
+1  A: 

G'day,

Shouldn't you be defining an accessor function instead if you're just "getting" the current value of the counter?

Something like:

int GetCounter();

Anything else is sort of disguising the intention of what you're trying to do. IMHO Natch! (-:

HTH

cheers,

Rob Wells
+7  A: 

As you have found out, the assignment operator must be a member function of a class. As ints are not classes, you can't write operator=() for them. The alternative, as others have pointed out is to write a function that converts to an int. I would strongly suggest you write a named function like ToInt() to do this, rather than using a conversion operator, which can be the source of non-obvious bugs.

anon
+1 for the explanation of why the operator= can not be used, and for the hint of explicit function. Go further than a code snippet ... (I've done that, shame on me :) )
neuro
rstevens
+1  A: 

As said use the int() operator. Here a 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;
}
neuro
A: 

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

Daniel Daranas
+1  A: 

You said:

"I've defined several operators like post/pre inc/dec returning an integer".

Now that other answers provided you with a generic way to convert the object to an integer, I would recommend that you change these other operators so that they behave as typically expected.

For instance, pre increment typically returns a reference to the object itself, and post increment typically returns a temporary copy of the original object (prior to the incrementation).

CCounter& operator++() {
    ++m_val;
    return *this;
}

CCounter operator++(int) {
    CCounter tmp(*this);
    ++m_val;
    return tmp;
}
Ropez