简体   繁体   中英

How to Deep Copy Values of Vector Pointer (Not just the pointer)?

I've an operator and function which goal is to copy the values of the vector pointer to a vector. I've read that as long it's a vector pointer, deep copying methods would only copy its pointer, not its value. I'm wondering how to copy it's values over instead. (The vector is a member of CObject class)

Operator function:

void CObject::operator=(CObject& rhs)
{
    this->ClearObject();   //Object will be cleared first
    // How to perform the deep copy below?
    copy(rhs.m_Vector.begin(), rhs.m_Vector.end(), back_inserter(this->m_Vector));
}

Declared as -> void operator=(CObject& rhs);

Example application of operator function (Performed in another class) :

CObject* m_pObjectOne;
CObject  m_ObjectTwo;

m_ObjectTwo = m_pObjectOne;

(Therefore, when m_pObjectOne is deleted in its class destructor, m_ObjectTwo loses its value)

Vector & others:

struct OBJECT_ITEM
{
    char  m_chType;
    UINT  m_nDataByte;
    BYTE* m_pData;
    CString m_strRecipeTag;
}

std::vector<OBJECT_ITEM> m_Vector;

I've also tried other deep copy methods like push_back() & assign() but it gives me the same result.

Any help would be appreciated!

Strive for the "rule of zero".

How to Deep Copy Values of Vector Pointer (Not just the pointer)?

Basically - don't. You should strive to follow the rule of zero : Unless otherwise necessary, set things up so that the default constructors, assignment operators and destructors do what they should.

In your case: Suppose you let CObject use its default (copy) assignment operator. That means, that rhs.m_Vector will be assigned to lhs.m_Vector . And vector assignment means that individual members of the vector are assigned to their corresponding members.

The way you wrote your vector, that won't do what you want: You wrote you want to avoid the case of "when m_pObjectOne is deleted in its class destructor, m_ObjectTwo loses its value)". Well, since you're willing to hold copies of the objects, consider something like the following:

struct OBJECT_ITEM
{
    char  m_chType;
    UINT  m_nDataByte;
    MyContainer m_upData;
    CString m_strRecipeTag;
}

And choose or write MyContainer to suit your needs. It could just be an std::vector<BYTE> ; or if you don't want the size to change after construction, use a dynarray (not in the standard library these days; here's an alternative ); etc. And your chosen container will actually get properly copied when you copy an OBJECT_ITEM . Finally, don't hold pointers to OBJECT_ITEM 's - just hold actual OBJECT_ITEM 's (which it seems you were already doing?)

And there, now you can no longer have dangling pointers - with no custom constructors, assignment operators or anything else like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM