#include <iostream>
#include <vector>
#include "AutoPtr.h"
#include "Resource.h"
using namespace std;
template<class T>
void MySwap(T& a, T& b)
{
T tmp{ std::move(a) };
a = std::move(b);
b = std::move(tmp);
}
int main()
{
{
AutoPtr<Resource> res1(new Resource(3));
res1->setAll(3); //-> operator로 m_ptr을 return
AutoPtr<Resource> res2(new Resource(5));
res2->setAll(5);
res1->print();
res2->print();
MySwap(res1, res2);
res1->print();
res2->print();
}
return 0;
}
#pragma once
#include <iostream>
class Resource
{
//private:
public:
int* m_data = nullptr;
unsigned m_length = 0;
public:
Resource()
{
std::cout << "Resource default constructed" << std::endl;
}
Resource(unsigned length)
{
std::cout << "Resource length constructed" << std::endl;
this->m_data = new int[length];
this->m_length = length;
}
Resource(const Resource& res)
{
std::cout << "Resource copy constructed" << std::endl;
Resource(res.m_length);
for (unsigned i = 0; i < m_length; ++i)
m_data[i] = res.m_data[i];
}
~Resource()
{
std::cout << "Resource dstroyed " << std::endl;
if (m_data != nullptr) delete[] m_data;
}
Resource& operator = (Resource& res)
{
std::cout << "Resource copy assignment" << std::endl;
if (&res == this) return *this;
if (this->m_data != nullptr) delete[] m_data;
m_length = res.m_length;
m_data = new int[m_length];
for (unsigned i = 0; i < m_length; ++i)
m_data[i] = res.m_data[i];
return *this;
}
void print()
{
for (unsigned i = 0; i < m_length; ++i)
std::cout << m_data[i] << " ";
std::cout << std::endl;
}
void setAll(const int& v)
{
for (unsigned i = 0; i < m_length; ++i)
m_data[i] = v;
}
};
#pragma once
#include <iostream>
template<class T>
class AutoPtr
{
public:
T* m_ptr;
public:
AutoPtr(T* ptr = nullptr) //default parameter 사용함
: m_ptr(ptr)
{
std::cout << "AutoPtr default constructor " << std::endl;
}
~AutoPtr()
{
std::cout << "AutoPtr destructor " << std::endl;
if (m_ptr != nullptr) delete m_ptr;
}
AutoPtr(const AutoPtr& a) //copy constructor //parameter로 l-value reference 사용
{
std::cout << "AutoPtr copy constructor " << std::endl;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
}
AutoPtr& operator = (const AutoPtr& a)
{
std::cout << "AutoPtr copy assignment " << std::endl;
if (&a == this)
return *this;
if (m_ptr != nullptr) delete m_ptr;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
return *this;
}
//AutoPtr(const AutoPtr& a) = delete;
//AutoPtr& operator = (const AutoPtr& a) = delete;
AutoPtr(AutoPtr&& a) //move constructor //r-value reference 사용
: m_ptr(a.m_ptr)
{
a.m_ptr = nullptr;
std::cout << "AutoPtr move constructor " << std::endl;
}
AutoPtr& operator = (AutoPtr&& a)
{
std::cout << "AutoPtr move assignment " << std::endl;
if (&a == this) //prevent self-assignment
return *this;
if (!m_ptr) delete m_ptr;
//shallow copy
m_ptr = a.m_ptr;
a.m_ptr = nullptr;
return *this;
}
T& operator*() const { return *m_ptr; }
T* operator->() const { return m_ptr; }
bool isNull() const { return m_ptr == nullptr; }
};