getAName(getA().get())
과 같은 함수 매개 변수를 전달하는 것이 안전합니까? getA()
개체를 반환 unique_ptr<A>
.C++ : unique_ptr :: get()과 같은 인수를 함수에 전달하는 것이 안전합니까?
아래 코드를 VS 2010에서 테스트합니다. 하지만 C++ 표준인지 확인하고 싶습니다. 다른 C++ 컴파일러에서 안전합니까? 콘솔
#include "stdafx.h"
#include <memory>
#include <iostream>
using namespace std;
class A
{
public:
A(){ cout<<"A()"<<endl;}
~A(){ cout<<"~A()"<<endl;}
string name() { return "A"; }
};
std::unique_ptr<A> getA()
{
return std::unique_ptr<A>(new A());;
}
void getAName(A* a)
{
if(a)
{
cout << a->name().c_str() << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
getAName(getA().get());
return 0;
}
출력은 다음과 같습니다
A()
A
~()
은 모든 컴파일러에 대해 다음과 같이 안전에 대한 코드를 할 필요가 있습니까?
unique_ptr<A> a = getA();
getAName(a.get());
cout을 사용하는 경우 c_str()가 필요하지 않습니다. – Kapil