아래 질문에 제 코드 첨부하겠습니다. 죄송합니다!
198
작성한 질문수 17
<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; }
};
답변 1
0
원인은 AtudoPtr 에 -> 연산자 오버로딩 안하셔서 그렇습니다!
AutoPtr 클래스 안에 T* operator->() const { return m_ptr; } 를 빠뜨리셨네요. 이 연산자를 통해 Resource 타입이 된 m_ptr 포인터를 리턴받아서 이걸로 Resource 멤버인 setAll 에 접근해야해요.
그리고 질문주신 내용은 아니지만 main 함수에서 res1.setAll(3) 으로 쓰셨는데 . 쩜이 아닌 -> 를 쓰셔야해요! res->setAll(3) 이렇게요!
빌드 하시고 오류 메세지 확인하시면 원인을 파악 할 수 있습니다. 참고해주세요 :)
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
461
1
메모리 주소 10진수로 출력
1
650
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
495
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
529
1
메모리 주소에 관한 질분
0
676
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
545
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
439
1
Digit 뒤에 reference를 사용하는 이유
0
504
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
319
1
dat파일이...
0
534
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
640
1
복사 생성자 관련 질문이 있습니다.
0
450
1
수업 중 궁금한점이 있습니다.
1
386
1
라이브러리자체가 이해가 되지 않습니다.
0
557
1
마지막 예제 질문
0
299
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
371
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
408
1
friend함수 관련 질문이 있습니다.
0
308
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
443
1
3분 17초 질문
0
346
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
442
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
440
2
const Something &st에서 const를 빼면 안되나요?
0
296
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
489
1





