Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. = operator in CObject-derived classes

= operator in CObject-derived classes

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dr Kuulun
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • D 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-Kuulun

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • M Michael Dunn

        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

        D Offline
        D Offline
        Dr Kuulun
        wrote on last edited by
        #3

        But the operator is represented by a function. Functions can be virtual. Why is it not possible for an operator? Now I wrote a virtual clone-function an the list class calls this function to copy the elements. This way works fine. Dr-Kuulun

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups