가장 좋은 샷 User
클래스의 shared_ptr<Resource>
멤버를 들고있다. 그러나이를 위해서는 무료 (힙) 메모리에 자원을 생성해야합니다.
class User {
User(Resource * res) : res_ptr(res) { //initialize user }
private:
shared_ptr<Resource> res_ptr;
}
void Init() {
Resource * res = new Resource(...);
User user = new User(res);
}
두 번째 방법은 Resource
에 대해 가짜 복사 생성자와 스왑 메커니즘을 제공하는 것입니다.
class Resource
{
private:
explicit Resource(const Resource &); // disabled to avoid heavy copy.
const Resource & operator = (const Resource &);
int * int_res;
public:
Resource() : int_res(new int(100)) { }
~Resource()
{
if(int_res != NULL)
delete int_res;
}
Resource(Resource & other) : int_res(NULL)
{
this->swap(other);
}
void swap(Resource & other)
{
using std::swap;
swap(int_res, other.int_res);
}
};
class User
{
private:
Resource resource;
User();
User(const User &);
const User & operator = (const User &);
public:
User(Resource & res) : resource(res) { }
~User() { }
};
그러나 사용자를 생성 한 후에는 리소스를 좀비 상태로 남겨두기 때문에 위험하고 오류가 발생하기 쉽습니다. 이 코드에서는 NULL
에 할당 된 int_res
포인터에 대한 모든 액세스가 액세스 위반 오류를 표시합니다. 내가 설명 할 수
마지막 방법은 "이동 의미의"C++ 0X 기능을 사용하고 있습니다.
class Resource
{
// ...
// ...
// Replace with Resource(Resource & other)
Resource(Resource && other) : int_res(NULL) //Move constructor
{
this->swap(other);
}
const Resource & operator = (Resource && other) // Move assignment operator
{
this->swap(other);
return *this;
}
void swap(Resource & other)
{
using std::swap;
swap(int_res, other.int_res);
}
};
class User
{
private:
Resource resource;
User();
User(const User &);
const User & operator = (const User &);
public:
User(Resource && res) : resource(std::move(res)) { }
~User() { }
};
void Init() {
Resource res(...);
User user = new User(std::move(res));
}
이 접근 방법은 다소 안전합니다. 포인터를 다룰 필요가 없으며 std::move
을 쓰는 것을 잊어 버린 경우 "Resource
인스턴스를 Resource&&
에 바인드 할 수 없습니다"또는 "의 복사 구성자가 비활성화 됨"이라는 컴파일러 오류가 발생합니다.
&&
개체가 일시적 강조하고 증발 것입니다. 따라서이 접근법은 Resource가 함수에 의해 생성되고 반환되는 시나리오에 더 적합합니다. 내가 너라면 나는 생성자를 비공개로 만들고 친구 함수로 생성자를 생성 할 것이다.
Resource GetResource(....)
{
Resource res(...);
return res;
}
void Init()
{
User user = new User(GetResource(...));
}
이 코드는 "이동 의미"와 완벽하게 일치합니다. C++ 0x 코드를 작성할 수 있으면 배우셔야합니다. This 및 this은 두 가지 좋은 동영상입니다.
'User' 객체간에'Resource' 객체를 공유하고 싶습니까, 아니면'User' 객체의 외부에 객체를 만들었습니까? – msandiford
나는 그것들을'User' 객체들 사이에서 공유 할 필요가 있습니다. –
디자인에 대해 더 알지 못하는 사이에 툴 체인의 나이에 따라 boost, tr1 또는 C++ 11 표준에서 사용할 수있는 shared_ptr과 같은 것을 원한다고 생각합니다. http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm – msandiford