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. Self assignment

Self assignment

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
4 Posts 4 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.
  • R Offline
    R Offline
    ragavan
    wrote on last edited by
    #1

    Hi Does anyone know how to prevent self assignment of object. E.g If I have a class A, A a; a = a; // how to prevent this

    P L M 3 Replies Last reply
    0
    • R ragavan

      Hi Does anyone know how to prevent self assignment of object. E.g If I have a class A, A a; a = a; // how to prevent this

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      Refer, http://www.parashift.com/c++-faq-lite/assignment-operators.html#faq-12.3[^].


      Prasad MS MVP -  VC++

      1 Reply Last reply
      0
      • R ragavan

        Hi Does anyone know how to prevent self assignment of object. E.g If I have a class A, A a; a = a; // how to prevent this

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

        ragavan wrote:

        a = a; // how to prevent this

        What do you mean "prevent"? You want a compiler error? If you just don't want the assignment to happen you can create an assignment operator and check if the right hand side value s address == this and do not perform the assignment.

        led mike

        1 Reply Last reply
        0
        • R ragavan

          Hi Does anyone know how to prevent self assignment of object. E.g If I have a class A, A a; a = a; // how to prevent this

          M Offline
          M Offline
          Moak
          wrote on last edited by
          #4

          You probably also want to prevent the copy constructor. I guess your question is regarding manual memory management, classes that handle memory/resources might need a decent copy constructor and an assignment operator (or alternatively a prevention of shallow copy). For details please see boost::noncopyable and the Law of the Big Two. In my class designs I sometimes take the easy way and just use 'noncopy' to prevent the shallow copy (which also means users can't make a copy of the objects, which really is fine for some classes). But sometimes you actually want this feature and then I implement both copy contructor and assignment operator (with a check for accidental self assignment). Hope it helps, M

          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