C++/CLI - Class assignment

Hi,

I'm currently converting apps from Managed C++ to C++/CLI. So far not so bad except that I'm not so sure about how to convert current operator+ assignment to C++/CLI syntax. In Managed C++, I had the following:

CL_Modules* CL_Modules::op_Assign(CL_Modules& Nouv_Module, const CL_Modules& Module_Src)
{
if(&Nouv_Module != &Module_Src)
{
Nouv_Module.Collection = Module_Src.Collection;
Nouv_Module.Module = Module_Src.Module;
Nouv_Module.Fig_Xpos = Module_Src.Fig_Xpos;
Nouv_Module.Fig_Ypos = Module_Src.Fig_Ypos;

// Copy infos vs graphicspath

Nouv_Module.Figure = dynamic_cast<GraphicsPath*>(Module_Src.Figure->Clone());

// Copy array vs Region(s)

Nouv_Module.FRegion = dynamic_cast<RectangleF[]>(Module_Src.FRegion->Clone());

// Copy infos vs "refresh area"

Nouv_Module.Rfrsh_Area = Module_Src.Rfrsh_Area;
}

return &Nouv_Module;
}

In C++/CLI, I tried to convert and got following result:

CL_Modules^ CL_Modules::operator =(const CL_Modules^ Module_Src)
{
Collection = Module_Src->Collection;
Module = Module_Src->Module;
Fig_Xpos = Module_Src->Fig_Xpos;
Fig_Ypos = Module_Src->Fig_Ypos;

// Copy infos vs graphicspath

Figure = dynamic_cast<GraphicsPath^>(Module_Src->Figure->Clone());

// Copy array vs Region(s)

FRegion = dynamic_cast<array<RectangleF>^>(Module_Src->FRegion->Clone());

// Copy infos vs "refresh area"

Rfrsh_Area = Module_Src->Rfrsh_Area;

return this;
}

Is my coding ok In my many books I have lots of example but nothing on assignment operator... :-(

Thanks for your help,

Stephane



Answer this question

C++/CLI - Class assignment

  • Duzinga

    Hi again,

      I noticed that your copy constructor looks different that the one I coded.

    The way I coded it:

    CL_Modules::CL_Modules(const CL_Modules^ Module_Src)
    {
         Collection = Module_Src->Collection;
         ...

    The way you suggest :

      public CL_Module(const CL_Module% obj) {
        // Copy obj members
        Collection = obj.Collection;
        ...

    Can you tell me what is the difference between using % compared to ^

    Thanks again,

    Stephane


  • Dmitry Arefyev

    Looks good, other than checking for self-assignment:
    if (Module_Src == this) return this;
    Problems



  • Charley Lou

    Sorry, I was messing around with operator=(), use yours with const CL_Module^.



  • Solitaire

    I could get an operator= overload to work but can't explain why it worked. The most "natural" way to accomplish what you need is to use a copy constructor. Write it like this:

    public ref class CL_Module {
    ..
    public CL_Module(const CL_Module% obj) {
    // Copy obj members
    Collection = obj.Collection;
    ...
    }
    };
    ...
    CL_Module^ pmov_module = gcnew CL_Module(psel_module);


  • eric_from_nj

    Hi again,

      I made some tests today and had unfortunately to realize that the "operator =" function coded like showed previously is giving me two variables with same physical address.  Here is the test I did:

          CL_Modules^ pmov_module = gcnew CL_Modules;
          pmov_module = psel_module;
          psel_module->Fig_Xpos *= 100;

      I then ran application into debug mode and had to rapidly discover that both pmov_module and psel_module got there Fig_Xpos value multiplied by 100 which is not what I would expect.  I would have ratherly expected that only psel_module's Fig_Xpos value to be modified.

      In "old" managed code, I was using the following call to the assignment routine

           *pmov_module = *psel_module;

      Now in "C++/CLI", how can I code my assignment routine so that only the content of class variable is copied to the other, that was my original question.  Any clue

    Thanks in advance,

    Stephane

    P.S. Following is a recall of the code for current C++/CLI assignment (operator =) routine I'm using:

    CL_Modules^ CL_Modules::operator =(const CL_Modules^ Module_Src)
    {
     if(this != Module_Src)
     {
       Collection =  Module_Src->Collection;
       Module     =  Module_Src->Module;
       Fig_Xpos   =  Module_Src->Fig_Xpos;
       Fig_Ypos   =  Module_Src->Fig_Ypos;

       // Copier les infos vs graphicspath

       Figure   = dynamic_cast<GraphicsPath^>(Module_Src->Figure->Clone());

       // Copier l'array vs Region(s)

       FRegion  = dynamic_cast<array<RectangleF>^>(Module_Src->FRegion->Clone());

       // Copier les infos sur la surface de Rafraichissement

       Rfrsh_Area = Module_Src->Rfrsh_Area;
     }

     return this;
    }


  • kawing0510

    Hi again,

      Just a very quick additional note to preceding reply.  I made some more debugging tests this evening to realize that the "operator=" routine is not called at all when issuing following command:

                     pmov_module = psel_module;

      Any idea on how to solve this problem

    Thanks all for your help,

    Stephane

     

     


  • Mohan1

    Hi,

    I already did a copy constructor using class object as input and it is working nicely but I use it only when I create a new object.

    Now if I want to copy object to another object which already exists, I think it would be more practical to have this "operator=" routine. Any hint

    When you say that you got that "operator=" to work, can you give me some clues on how could do it

    Thanks again,

    Stephane


  • ceibner

    Hi,

      Many thanks for replying.  My question was based on the fact that I didn't have any example on how to do it.  I did coding based on an quick example with "operator -" that I could find in my C++/CLI book.  I realized that final result was quite different from the one I did with Managed code.  Honestly, I'm still not quite sure about when to use ^ or * or & with C++/CLI.  Mechanism seems quite different.  Any clue that might help

      Finally, am I right assuming that coding the assignment routine this way, resulting assignment should be done this way:

          CL_Modules^  Module_to_copy;
          CL_Modules^  Dest_Module;

          Dest_Module = Module_to_copy;           

                      or

         *Dest_Module = *Module_to_copy;          
     

    Thanks again,

    Stephane

    P.S.  Actually using a french keyboard layout, does anyone of you have any good trick on how I can get the "^" caracter in a more easier manner   On a french keyboard layout, the "^" caracter is what we call a caracter "accent".  To get this caracter displayed, I always have to stike corresponding key twice but this gives me two caracters instead of one so that I have then to strike "backspace" key to erase one which is quite anoying when coding routine...  Any good trick

     


  • C++/CLI - Class assignment