3

The title basically says it all. I mainly want to do this so that I can create an object (say, a custom string object) that can initialize the parameters of other functions in other APIs. Here's an example of me trying to get a custom integer class to work:

#include <iostream>
using namespace std;

class test
{
public:
    int member;
    test(int i) : member(i) {}

    friend int &operator=(int &i, test t);
};

int &operator=(int &i, test t)
{
   return (i = t.member);
}

int main()
{
    int i;
    test t = 90;

    cout << (i = t);
    return 0;
}

Unfortunately I receive an error saying that operator= needs to be a member function. I understand the C++ standard's goal in preventing static and non-member overloads for the assignment operator from being implemented, but is there any other way to do this? Thanks for any help/suggestions!

Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
AutoBotAM
  • 1,485
  • 3
  • 18
  • 24

4 Answers4

5

This is not done with an assignment operator but with an overloaded typecast. This would make your main function work like expected:

#include <iostream>
using namespace std;

class test
{
public:
    int member;
    test(int i) : member(i) {}
    operator int() const {return member;}
};

int main()
{
    int i;
    test t = 90;

    cout << (i = t);
    return 0;
}
manol
  • 572
  • 3
  • 13
  • 2
    +1, though `operator int()` should be `const`, and `test(int)` safer (albeit sometimes less convenient - `test t = 90`) if `explicit`. – Tony Delroy Jul 27 '11 at 05:36
3

What you are trying to do needs an conversion operator

operator int() 
{
    return this->member;
}

For the class you are trying to write(containing only integer members), You do not need to overload the = operator.

= operator is one of the member functions that is generated by the compiler by default for every class. Caveat is, it does a simple bit by bit copy(shallow copy) of class members, since you have only integers it should be good enough for you.

You would need to overload the = operator if you had dynamically allocated pointers as member functions, because in that case a shallow copy of those pointers would result in all the objects containing a member pointer pointing to the same dynamic memory location & if one of the object finishes it lifetime, other objects are left with a dangling pointer.
As @Tony, aptly points in out comments Shallow copy is usually bad but not always. See his comments for a scenario.

If at all you want to overload the assignment operator check out the Copy and Swap Idiom to do it right way.

You should also check out the Rule of Three.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    "[operator=] does a simple bit by bit copy (shallow copy) of operands"... operands is a strange term to use... perhaps member variables? And it invokes copy constructors for members that have them - they may not do bit-by-bit shallow copies (e.g. `std::string`). Re shallow copy of pointers being dangerous: that's usually true, but not always - e.g. (typically `const`) pointers to static reference data can be copied safely. – Tony Delroy Jul 27 '11 at 05:41
  • 1
    @Tony: Thanks! I Changed that bit about operands to members. Admit, that was not so verbose. I was referring to most usual case of member pointers with dynamic memory allocation because that is where an default assignment over overloaded one hurts newbies most. Your comment should help decipher the exception to ones reading the answer though, So thanks :) – Alok Save Jul 27 '11 at 05:51
  • 1
    yes - I could follow your thinking, just worried "newbies" might think the "any pointer = need your own operator=" guideline was more of a rule. It's a good guideline though, and as you say helps newbies. Dynamic memory's another danger sign, though not 100% correlated: e.g. the pointed-to object may be returned by a factory method called in `main()` and only deleted shortly before application exit, while lifetime of objects pointing to it are "contained" therein). Cheers. – Tony Delroy Jul 27 '11 at 06:20
  • Ahh, this does make sense now. Always good to be aware of the operator overloads, which I still need a bit of review on. Thanks! – AutoBotAM Jul 27 '11 at 18:08
3

Try this:

class test
{
public:
    int member;
    test(int i) : member(i) {}

    operator int() {return this->member;}
};

int main(void)
{
    int i;
    test t = 90;

    cout << (i = t);
    return 0;
}
Brain2000
  • 4,655
  • 2
  • 27
  • 35
1

The assignment operator cannot be a friend function. The assignment operator can only be declared as a non-static member function. This is to ensure that it receives the L-value as its first operand. The same is true for the [], (), and -> operators. In your case, since int is an build-in type, you cannot use member function. You can implement operator int() to cast your user-defined type to int.

Eric Z
  • 14,327
  • 7
  • 45
  • 69