#include "pch.h"
#include <iostream>
usingnamespace std;
class Example6
{
int *value;
public:
Example6() {};
Example6(int x) : value(newint(x)) { }
~Example6() { delete value; }
Example6(Example6 &val) : value(val.value) { val.value = nullptr; }
Example6& operator= (Example6& x)
{
delete value;
value = x.value;
x.value = nullptr;
return *this;
}
int returneazaVal() { return *value; }
Example6 operator+(Example6 &temp)
{
Example6 t(returneazaVal() + temp.returneazaVal());
return t;
}
};
int main()
{
int a = 2, b = 5;
Example6 t(a);
Example6 h(b);
Example6 val;
val = t + h; // this line problem
cout << t.returneazaVal();
return 0;
}
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'Example6'
And I have another question
I see in tutorial:
const string& content() const {return data;}
what is this const ... const? some reference?
why is Example6& operator= (Example6& x) "Example6 & operator..." this & and this
Example6 operator+(Example6 &temp) "&temp" don't work without &?
I know is referal, but i need to know thinking backwards
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'Example6'
The problem is that the operator=(Example6& x) expects a reference of that object. The operator+(Example6 &temp) returns a temporary object that cannot used by non const reference.
what is this const ... const? some reference?
When used with an object const string&: the object shall not be modified.
When used with a member function content() const: The member function shall not modify any member variables.
why is Example6& operator= (Example6& x) "Example6 & operator..." this & and this
Example6 operator+(Example6 &temp) "&temp" don't work without &?
It actually works without '&'. With '&' it is passed by reference, without it is passed by value.