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. A question about OOP

A question about OOP

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 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.
  • N Offline
    N Offline
    nguyenvhn
    wrote on last edited by
    #1

    What's the difference between 2 below implementations? Case 1: class animal{ animal& operator=(animal& ani){ //few operations return *this; } } Case 2: class animal{ animal operator=(animal& ani){ //few operations return *this; } } Please note the reference sign (&) in return type of overloading operator. Which implementation is correct? Best regards.

    J T 2 Replies Last reply
    0
    • N nguyenvhn

      What's the difference between 2 below implementations? Case 1: class animal{ animal& operator=(animal& ani){ //few operations return *this; } } Case 2: class animal{ animal operator=(animal& ani){ //few operations return *this; } } Please note the reference sign (&) in return type of overloading operator. Which implementation is correct? Best regards.

      J Offline
      J Offline
      Jens Doose
      wrote on last edited by
      #2

      Case 1 is correct, because it returns a reference instead of a copy of this object. See also: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/overl_14.asp[^]

      1 Reply Last reply
      0
      • N nguyenvhn

        What's the difference between 2 below implementations? Case 1: class animal{ animal& operator=(animal& ani){ //few operations return *this; } } Case 2: class animal{ animal operator=(animal& ani){ //few operations return *this; } } Please note the reference sign (&) in return type of overloading operator. Which implementation is correct? Best regards.

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        when you return a reference, you will be able tou chain the operators like this:

        animal MyAnimal1, MyAnimal2, MyAnimal3; // Declarations
        MyAnimal1 = animal(/*accepted constructor parameters*/); // here, both of the operator=() could work
        MyAnimal3 = MyAnimal2 = MyAnimal1; // here, it must be a reference returned

        in general, we prefer the synopsis **T& operator= (const T&)**... make your choice:)


        TOXCCT >>> GEII power

        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