Need help on how to implement c++ move semantic

Am learning how to implement c++ move semantic, i need help. The code below are my code so far. It's pouring out error which I can't figure out. Thanks in advance.
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
#include <iostream>

using namespace std;

class A
{
    private:
    int a;
    
    public:
    A();
    ~A();
    A(int ab)
    {
       this->a=ab;   
    }
    A(A&& ac)
    {
       this->a=ac.a;
       ac.a=0;
    }
    A& operator = (A&& ad)
    {
        this->a=ad.a;
        ad.a=0;
        return *this;
    }
};

int main()
{
    A a1(5);
    A a2=a1;
    cout<<a1<<endl;
    cout<<a2<<endl;
    return 0; 
}
Last edited on
You need to declare and define a copy constructor (because that's actually what you are using on line 33).

You need to declare and define a friend ostream & operator << or your lines 34 and 35 aren't going to work.

You have declared both zero-parameter constructor and the destructor ... but you haven't defined either of them.

I'm not convinced that you are moving anything - that would involve reassigning memory - usually dynamically-allocated memory - to another variable. You are, however, doing a lot of copying ... which seems to defeat the object.


See:
http://www.cplusplus.com/doc/tutorial/classes2/
under "special members".
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
include <iostream>

using namespace std;

class A
{
private:
	int a {};

public:
	A() {}
	A(int ab) : a(ab) {}
	A(A&& ac) : a(ac.a) {
		ac.a = 0;
	}

	A& operator=(A&& ad)
	{
		a = ad.a;
		ad.a = 0;
		return *this;
	}

	friend ostream& operator<<(ostream& os, const A& aa);
};

ostream& operator<<(ostream& os, const A& aa)
{
	return os << aa.a;
}

int main()
{
	A a1(5);
	A a2 = A(6);
	cout << a1 << '\n';
	cout << a2 << '\n';
}


Note that

 
A a2 = a1;


is NOT using move semantics. That is a copy constrictor, not a move constructor. A move constructor is where the assigned value is a temp as in

 
A a2 = A(6);

Last edited on
> Am learning how to implement c++ move semantic, i need help.

Do nothing at all.
For that particular class (and for most classes that you would be writing), do nothing special;
the implicitly defined (compiler generated) move constructors and move assignment operators would do the right thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <type_traits>

class A
{
    private: int a;

    public: explicit A( int v = 0 ) noexcept : a(v) {}
};

int main()
{
    std::cout << std::boolalpha << "Type A:\n-------\n"

              << "move_constructible: " << std::is_move_constructible_v<A> << '\n' // true
              << "no_throw_move_constructible: " << std::is_nothrow_move_constructible_v<A> << '\n' // true
              << "trivially_move_constructible: " << std::is_trivially_move_constructible_v<A> << '\n' // true
              << '\n'
              << "move_assignable: " << std::is_move_assignable_v<A> << '\n' // true
              << "no_throw_move_assignable: " << std::is_nothrow_move_assignable_v<A> << '\n' // true
              << "trivially_move_assignable: " << std::is_trivially_move_assignable_v<A> << '\n' ; // true
}

http://coliru.stacked-crooked.com/a/8890560eccff3d5c

We need to write code to support move semantics if and only if:
The implicitly-defined special member functions are typically incorrect if the class manages a resource whose handle is an object of non-class type (raw pointer, POSIX file descriptor, etc), whose destructor does nothing and copy constructor/assignment operator performs a "shallow copy" (copy the value of the handle, without duplicating the underlying resource).
https://en.cppreference.com/w/cpp/language/rule_of_three


The rule of zero:
Classes that have custom destructors, copy/move constructors or copy/move assignment operators should deal exclusively with ownership (which follows from the Single Responsibility Principle). Other classes should not have custom destructors, copy/move constructors or copy/move assignment operators
https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero

Core Guidelines:
If you can avoid defining default operations, do (avoid)
Reason: It’s the simplest and gives the cleanest semantics.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-zero
Topic archived. No new replies allowed.