= operator in CObject-derived classes
-
Hello, I wrote one post this day to my problem. Now I am able to describe my problem more detailed. So once more: I want to write a base class and a class that manage a list of objects derived from my base class. The list-class should have a = operator that copies the whole list and creates copies of all its elements (derived from base class). The base class is derived from CObject because of serialisation and so on. It also includes some basic methods and members.
class CBase::Cobject { .... }
Now I derive some classes from it.class CA::CBase { } class CB::CBase { }
All classes should have a = operator to copy its elements! The list-class now have a list of pointers to some instances from CA und CB in a CObArray. The = operator of the list-class shoud create a copy of the list an the members.CBase* pElement1; CBase* pElement2; CRuntimClass* pRTC; int nIndex; for (nIndex=0; nIndexGetCount(); nIndex++) { pElement1 = pList->GetAt(nIndex); pRTC = pElement1->GetRuntimeClass(); pElement2 = (CBase*)pRTC->CreateObject(); pRTC = pElement2->GetRuntimeClass(); *pElement2 = *pElement1; Add(pElement2); }
pList is a pointer to the source CObArray-Object. Add() adds an element in the destination CObArray-Object. Now the problem: *pElement2 = *pElement1 do not call the = operator of CA or CB. The = operators have no virtual behaviour. The result ist that not all elements (CBase and CA/CB) are copied. Only the = operator of CBase is called. What is to do? And how can i call in the = operator function of CA the = operator funrction of CBase to copy the base elements? And one more question: What is the reason for the Cxyz& return type of the = operators and the return(*this) statement? Thanx Dr-Kuulun -
Hello, I wrote one post this day to my problem. Now I am able to describe my problem more detailed. So once more: I want to write a base class and a class that manage a list of objects derived from my base class. The list-class should have a = operator that copies the whole list and creates copies of all its elements (derived from base class). The base class is derived from CObject because of serialisation and so on. It also includes some basic methods and members.
class CBase::Cobject { .... }
Now I derive some classes from it.class CA::CBase { } class CB::CBase { }
All classes should have a = operator to copy its elements! The list-class now have a list of pointers to some instances from CA und CB in a CObArray. The = operator of the list-class shoud create a copy of the list an the members.CBase* pElement1; CBase* pElement2; CRuntimClass* pRTC; int nIndex; for (nIndex=0; nIndexGetCount(); nIndex++) { pElement1 = pList->GetAt(nIndex); pRTC = pElement1->GetRuntimeClass(); pElement2 = (CBase*)pRTC->CreateObject(); pRTC = pElement2->GetRuntimeClass(); *pElement2 = *pElement1; Add(pElement2); }
pList is a pointer to the source CObArray-Object. Add() adds an element in the destination CObArray-Object. Now the problem: *pElement2 = *pElement1 do not call the = operator of CA or CB. The = operators have no virtual behaviour. The result ist that not all elements (CBase and CA/CB) are copied. Only the = operator of CBase is called. What is to do? And how can i call in the = operator function of CA the = operator funrction of CBase to copy the base elements? And one more question: What is the reason for the Cxyz& return type of the = operators and the return(*this) statement? Thanx Dr-Kuulunoperator=
is not inherited and (I think) cannot be virtual. Polymorphism wouldn't apply to an assignment because the compiler has to know exactly what types are involved in an assignment. Each class is responsible for its own copying/assignment behavior.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
operator=
is not inherited and (I think) cannot be virtual. Polymorphism wouldn't apply to an assignment because the compiler has to know exactly what types are involved in an assignment. Each class is responsible for its own copying/assignment behavior.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ