Problem for move and assigned code

I try to make move and assignment class, for understand this special member, but i have a problem:

I don't hava idee how to make a better conversion

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "pch.h"
#include <iostream>

using namespace std;

class Example6 
{
	int *value;
public:
	Example6() {};
	Example6(int x) : value(new int(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
Last edited on
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.

Topic archived. No new replies allowed.