2014-04-14 1 views
0

아래에 표시된 소스 코드는 Document 클래스의 개체 멤버를 확인하는 일부입니다. 'memberObject'인 값 클래스의 개체를 만들고 '_value'값 참조 개인 멤버 변수에 개체 참조를 저장하려고했습니다. 출력을 보면 개체 유형이 3 (Object)임을 알 수 있습니다. 그러나 _value 참조 변수에 memberObject를 할당하면 출력에 유형이 0 (NULL)으로 변경된 것으로 표시됩니다. 이와 같은 형식 변경이 발생하지 않아야합니다. 왜 그런지 설명해 주시겠습니까?왜 rapidjson에서 대입 연산자를 호출 한 후 멤버 변수가 변경됩니까?

for (Value::MemberIterator itr = _document.MemberBegin(); itr != _document.MemberEnd(); itr++) 
    { 
     _itr = itr; 

     _name = itr->name.GetString(); 
     _objectTypeID = (int)itr->value.GetType(); 

     cout << "Member [" << _name << "] - type is [" << _objectTypeID << "]" << endl; 

     _typeID = _objectTypeID; 

     if (itr->value.IsObject()) 
     { 
       Value& memberObject = _document[_name.c_str()]; 
       cout << "Value type(1): " << memberObject.GetType() << endl; 

       _value = (Value&)memberObject; 
       cout << "Value type(2): " << memberObject.GetType() << endl; 
     } 


     _st.push(_itr); 

     parseValue(); 

     _itr = _st.top(); // returns the next element in the stack 
     _st.pop();   // removes an element from the stack 
    } 

"firmwareSettings": { 
    "manageFirmware": false, 
    "firmwareBaselineUri": "" 
}, 

회원 [firmwareSettings] - 타입 [3]
값 유형 (1) : 3
값 유형 2 : 0

답변

1

이 동작은 GenericValue의 할당 연산자가 의미 이동을 사용하므로 예상됩니다. 여기

rapidjson대한 할당 연산자 GenericValue이다

//! Assignment with move semantics. 
    /*! \param rhs Source of the assignment. It will become a null value after assignment. 
    */ 
    GenericValue& operator=(GenericValue& rhs) { 
     RAPIDJSON_ASSERT(this != &rhs); 
     this->~GenericValue(); 
     memcpy(this, &rhs, sizeof(GenericValue)); 
     rhs.flags_ = kNullFlag; 
     return *this; 
    } 

memberObject는 인 flags_ 부재를 변화에 _value, 할당 연산자 차기 할당되면 rvalue 객체의 GetType() 메서드에서 반환하는 값입니다.

이동 의미에 대한 자세한 내용은 What are move semantics?

을 참조하십시오.