I just made a wrapper for move and copy operations to enter into the code to see what is called in the case of the default implementation. I am getting closer to understanding what is called, but I would like to double-check the time.
I'm not sure if method 1 of is using T::T;better for constructors than method 2 to forward arguments like unique_ptr? I found it in this thread. Forwarding all constructors in C ++ 0x
In the move constructor and assignment, I use std::moveto go to the superclass. Should it be std::forward, and if so, how? I get errors trying to use it.
#ifndef MOVECOPY_OBSERVER_H
#define MOVECOPY_OBSERVER_H
#include <iostream>
template<class T>
class MoveCopyObserver : public T {
public:
using T::T;
virtual ~MoveCopyObserver() = default;
MoveCopyObserver(const MoveCopyObserver& other)
: T(other)
{
std::cout << "Copy constructor " << typeid(T).name() << std::endl;
}
MoveCopyObserver(MoveCopyObserver && other)
: T(std::move(other))
{
std::cout << "Move constructor " << typeid(T).name() << std::endl;
}
MoveCopyObserver& operator=(const MoveCopyObserver& other)
{
T::operator=(other);
std::cout << "Copy assignment " << typeid(T).name() << std::endl;
return *this;
}
MoveCopyObserver& operator=(MoveCopyObserver&& other)
{
T::operator=(std::move(other));
std::cout << "Move assignment " << typeid(T).name() << std::endl;
return *this;
}
};
#endif
Usage will be on the stack or through smart pointers, for example:
class A {
public:
A(std::string ss)
{
s = ss;
}
void f()
{
std::cout << "\"" << s << "\"" << std::endl;
}
private:
std::string s;
};
A a("Test instance");
a.foo();
MoveCopyObserver<A> b("Another instance");
b.foo();