2012-03-16 2 views
1

* graph1이라는 그래프 포인터가 있고 메모리가 이미 할당되어 있습니다 (참고 : 질문의 일부가 아니지만 Graph는 템플릿 클래스 임). 그래프 2라는 또 다른 그래프 인스턴스를 만들었습니다. 나는 할당 연산자가 제대로 작동 그래서소멸자 호출 AFTER 오버로드 된 할당 연산자 - C++

Graph<std::string,std::string> *graph1 = new Graph<std::string,std::string>; 
... 
... // Called member functions on graph1 
Graph<std::string,std::string> graph2; 
graph2 = *graph1; 

처럼 그들에 오버로드 된 할당 연산자라고하지만 어떤 이유로 그래프의 소멸자는 할당 연산자가 호출 된 직후에 호출됩니다. 이것은 정상입니까, 아니면 할당 연산자를 제대로 구현하지 않았습니까? , 임시의 불필요한 생성을 생성하는 수익 유형으로 Graph<VertexType, EdgeType>를 사용하여

template <typename VertexType, typename EdgeType> 
Graph<VertexType, EdgeType> Graph<VertexType, EdgeType>::operator=(const Graph<VertexType, EdgeType> &source) 
{ 
    std::cout << "\tAssignment Operator called\n\n"; 
    if(this == &source) 
    return *this; 

    this->vecOfVertices = source.vecOfVertices; 
    this->orderPtr = source.orderPtr; 
    this->count = source.count; 
    return *this; 
} 
+0

소멸자가 실행중인 개체를 알고 있습니까? – Aatch

답변

6

할당 연산자의 정확한 정의는

Graph<VertexType, EdgeType>& Graph<VertexType, EdgeType>:: 
    operator=(const Graph<VertexType, EdgeType> &source); 

입니다 :

내가 할당 연산자를 구현하는 방법이다 변하기 쉬운.

+0

와우. 나는 이걸 직접 봐야했고, 어디에서나 템플릿 매개 변수를 작성해야하는 것과 혼동을 느꼈습니다. 당신의 도움을 주셔서 감사합니다! – jbisa