복사 생성자가 호출되지 않고 이유가 확실하지 않습니다. 여기 내 코드 :템플릿 클래스 복사 생성자가 호출되지 않았습니다.
template <typename T>
class SmartPtr
{
public:
explicit SmartPtr(T *p) : m_p(p) { cout << "ctor" << endl; }
SmartPtr(const SmartPtr& p) : m_p(p.m_p) { cout << "copy ctor" << endl;}
private:
T* m_p;
};
int main()
{
SmartPtr<int> pt4 = SmartPtr<int>(new int);
}
출력은 "ctor"입니다. 기본 복사 생성자가 사용 된 것 같습니다. "명시 적"을 추가하면 오류가 발생하여 컴파일되지 않습니다.
"error: no matching function for call to ‘SmartPtr<int>::SmartPtr(SmartPtr<int>)’"
여기서 내가 뭘 잘못하고 있니?
. "copy elision"으로 검색하십시오. – juanchopanza
[가능한 경우 복사 생성자가 호출되지 않는 이유는 무엇입니까?] (http://stackoverflow.com/questions/1758142/why-copy-constructor-is-not-called-in-thiscase) – stefaanv
@stefaanv 처음 여기 나는 받아 들여진 대답이 실제로 틀린 것을 보았습니다 ... – Barry