Written on
·
189
0
<main source>
#include "AutoPtr.h"
#include "Resource.h"
#include <utility> //std::move
#include <vector>
#include <string>
template<class T>
void MySwap(T &a, T &b)
{
T tmp = a;
a = b;
b = tmp;
/*T tmp{ std::move(a) };
a = std::move(b);
b = std::move(tmp);*/
}
int main()
{
using namespace std;
//예제2)
{
AutoPtr<Resource> res1(new Resource(3));
res1.setAll(3);
AutoPtr<Resource> res2(new Resource(5));
res2.setAll(5);
MySwap(res1, res2);
res1->print();
res2->print();
}
}
***************************************************
<Resource>
#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 destroyed" << 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;
}
};
***************************************************
<Autoptr>
#include <iostream>
template<class T>
class AutoPtr
{
//private:
public:
T *m_ptr;
public:
AutoPtr(T*ptr = nullptr)
:m_ptr(ptr)
{
std::cout << "AutoPtr default constructor" << std::endl;
}
~AutoPtr()
{
std::cout << "AutoPtr destructor" << std::endl;
if (m_ptr != nullptr) delete m_ptr;
}
//copy constructor
AutoPtr(const AutoPtr& a)
{
std::cout << "AutoPtr copy constructor" << std::endl;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
}
//copy assignment
AutoPtr& operator = (const AutoPtr& a)
{
std::cout << "AutoPtr copy assignment" << std::endl;
if (&a == this) //prevent self-assignment
return *this;
if (m_ptr != nullptr) delete[] m_ptr;
//deep copy
m_ptr = new T;
*m_ptr = *a.m_ptr;
return *this;
}
//copy constructor나 copy assignment operator을 강제로 사용하지 않게 할 때
//AutoPtr(const AutoPtr& a) = delete;
//AutoPtr& operator=(const AutoPtr& a) = delete;
AutoPtr(AutoPtr&& a)
: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; }
};
Answer 1
0
원인은 AtudoPtr 에 -> 연산자 오버로딩 안하셔서 그렇습니다!
AutoPtr 클래스 안에 T* operator->() const { return m_ptr; } 를 빠뜨리셨네요. 이 연산자를 통해 Resource 타입이 된 m_ptr 포인터를 리턴받아서 이걸로 Resource 멤버인 setAll 에 접근해야해요.
그리고 질문주신 내용은 아니지만 main 함수에서 res1.setAll(3) 으로 쓰셨는데 . 쩜이 아닌 -> 를 쓰셔야해요! res->setAll(3) 이렇게요!
빌드 하시고 오류 메세지 확인하시면 원인을 파악 할 수 있습니다. 참고해주세요 :)