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. Managed C++/CLI
  4. Very simple question about gcnew in opreator overloading cases

Very simple question about gcnew in opreator overloading cases

Scheduled Pinned Locked Moved Managed C++/CLI
c++questiontutoriallearning
4 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.
  • A Offline
    A Offline
    aa1ww
    wrote on last edited by
    #1

    Consider the unary operator overload as per the example in the Stephen G. Fraser book, "Pro Visual C++/CLI", p. 114: . . static OpClass^ operator -(const OpClass ^lhs) { OpClass^ ret == gcnew OpClass(); // gcnew invoked here. ret->i = -(lhs->i); return ret; } . . Let's say I have previously instantiated OpClass objects, fred and jane as follows: OpClass^ fred = gcnew OpClass(); fred->i = 15; OpClass^ jane = gcnew OpClass(); jane->i = 23; fred = -jane; ^ | +---- This causes the "ret" to be instantiated as a clsInt object and a copy ( by default memberwise copy? ) is assigned to fred^. Does ret's going out of scope at the close of the overloaded operator function call mark ret's space on the managed heap for garbage collection? And, if this were just traditional C++ would the ret's going out of scope mark it for "delete"ion or would I have to explicitly delete it (in my ancient Deitel & Deitel he doesn't delete it)? Thanks, in advance, Coop

    Jeff Cooper

    L 1 Reply Last reply
    0
    • A aa1ww

      Consider the unary operator overload as per the example in the Stephen G. Fraser book, "Pro Visual C++/CLI", p. 114: . . static OpClass^ operator -(const OpClass ^lhs) { OpClass^ ret == gcnew OpClass(); // gcnew invoked here. ret->i = -(lhs->i); return ret; } . . Let's say I have previously instantiated OpClass objects, fred and jane as follows: OpClass^ fred = gcnew OpClass(); fred->i = 15; OpClass^ jane = gcnew OpClass(); jane->i = 23; fred = -jane; ^ | +---- This causes the "ret" to be instantiated as a clsInt object and a copy ( by default memberwise copy? ) is assigned to fred^. Does ret's going out of scope at the close of the overloaded operator function call mark ret's space on the managed heap for garbage collection? And, if this were just traditional C++ would the ret's going out of scope mark it for "delete"ion or would I have to explicitly delete it (in my ancient Deitel & Deitel he doesn't delete it)? Thanks, in advance, Coop

      Jeff Cooper

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      aa1ww wrote:

      Does ret's going out of scope at the close of the overloaded operator function

      it doesn't go out of scope, it's returned

      aa1ww wrote:

      return ret; }

      aa1ww wrote:

      if this were just traditional C++ would the ret's going out of scope mark it for "delete"ion

      Native C++ has no garbage collection so there is no marking of anything for deletion.

      led mike

      A 1 Reply Last reply
      0
      • L led mike

        aa1ww wrote:

        Does ret's going out of scope at the close of the overloaded operator function

        it doesn't go out of scope, it's returned

        aa1ww wrote:

        return ret; }

        aa1ww wrote:

        if this were just traditional C++ would the ret's going out of scope mark it for "delete"ion

        Native C++ has no garbage collection so there is no marking of anything for deletion.

        led mike

        A Offline
        A Offline
        aa1ww
        wrote on last edited by
        #3

        Ok, bear with me here... fred is a handle to some resources gcnew'd in the instantiation of an OpClass object in the main program, right? Then, during the operator overloading function call, more resources were gcnew'd for the instantiation of OpClass (whose handle is ret). So ret is returned as a handle and fred (also an OpClass handle) is replaced, OK. But there are now two allocations of OpClass objects on the managed heap. perhaps the CLR notices that the previously allocated resources pointed to by fred before the overloading function call are now "orphaned" (when fred was replaced by ret) and can mark those resources for deletion? Thanks, Coop PS: Right, traditional C++ requires me to do explicit delete's on my allocated resources.

        Jeff Cooper

        L 1 Reply Last reply
        0
        • A aa1ww

          Ok, bear with me here... fred is a handle to some resources gcnew'd in the instantiation of an OpClass object in the main program, right? Then, during the operator overloading function call, more resources were gcnew'd for the instantiation of OpClass (whose handle is ret). So ret is returned as a handle and fred (also an OpClass handle) is replaced, OK. But there are now two allocations of OpClass objects on the managed heap. perhaps the CLR notices that the previously allocated resources pointed to by fred before the overloading function call are now "orphaned" (when fred was replaced by ret) and can mark those resources for deletion? Thanks, Coop PS: Right, traditional C++ requires me to do explicit delete's on my allocated resources.

          Jeff Cooper

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          aa1ww wrote:

          perhaps the CLR notices that the previously allocated resources pointed to by fred before the overloading function call are now "orphaned" (when fred was replaced by ret) and can mark those resources for deletion?

          It better since that is what a Garbage Collection systems primary purpose is. ;)

          led mike

          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