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. which is better and why???

which is better and why???

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 7 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
    namaskaaram
    wrote on last edited by
    #1

    consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)

    W L V T J 5 Replies Last reply
    0
    • N namaskaaram

      consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)

      W Offline
      W Offline
      Wim Engberts
      wrote on last edited by
      #2

      The efficiency of the two does not realy differ very much. Of course, the first way comes with some additional machine instructions. It mainly depends on what you want to do with your constructor. William

      1 Reply Last reply
      0
      • N namaskaaram

        consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)

        L Offline
        L Offline
        Laxman Auti
        wrote on last edited by
        #3

        namaskaaram wrote:

        which one of the above is better??(in terms of efficiency)

        I think first one is good to direct the reference correctly. As well we can give the gurantee of consistency in data. Knock out 't' from can't, You can if you think you can :cool:

        1 Reply Last reply
        0
        • N namaskaaram

          consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          Actually "copy constructor" is this one: C(const C & ); Regarding overloading assignment operators, I think your first variant, which returns a reference, allows multiple assignments like this: a = b = c, or compact expressions like this: f(a = b). The second one does not allow this, but is little faster because of missing return value. -- modified at 4:24 Tuesday 20th June, 2006

          N J 2 Replies Last reply
          0
          • V Viorel

            Actually "copy constructor" is this one: C(const C & ); Regarding overloading assignment operators, I think your first variant, which returns a reference, allows multiple assignments like this: a = b = c, or compact expressions like this: f(a = b). The second one does not allow this, but is little faster because of missing return value. -- modified at 4:24 Tuesday 20th June, 2006

            N Offline
            N Offline
            namaskaaram
            wrote on last edited by
            #5

            actually i meant assignment operator overloading....sorry abt the typo!:doh: ;o) thank u all!!!! :)

            1 Reply Last reply
            0
            • N namaskaaram

              consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)

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

              in fact, prefer the following operator :

              C& C::operator = (const C&);

              the reason to prefer this one instead of the void operator is that the user of your class will then be able to cascade the assignment operations. consider this :

              int i1, i2, i3;
              C c1, c2, c3;

              i1 = i2 = i3 = 5;
              c1 = c2 = c3 = /*...*/;

              this is possible only because the operator = returns a reference to the object it has just modified...


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

              K 1 Reply Last reply
              0
              • T toxcct

                in fact, prefer the following operator :

                C& C::operator = (const C&);

                the reason to prefer this one instead of the void operator is that the user of your class will then be able to cascade the assignment operations. consider this :

                int i1, i2, i3;
                C c1, c2, c3;

                i1 = i2 = i3 = 5;
                c1 = c2 = c3 = /*...*/;

                this is possible only because the operator = returns a reference to the object it has just modified...


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #7

                Yes, you're right. The rationale is that the assignment operator should behave in the same way as assignment for primitive types. Kevin

                1 Reply Last reply
                0
                • N namaskaaram

                  consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)

                  J Offline
                  J Offline
                  Jorgen Sigvardsson
                  wrote on last edited by
                  #8

                  namaskaaram wrote:

                  i believe copy constructor can be written in two wayz:

                  That is not a copy constructor, it's an assignment operator.

                  1 Reply Last reply
                  0
                  • V Viorel

                    Actually "copy constructor" is this one: C(const C & ); Regarding overloading assignment operators, I think your first variant, which returns a reference, allows multiple assignments like this: a = b = c, or compact expressions like this: f(a = b). The second one does not allow this, but is little faster because of missing return value. -- modified at 4:24 Tuesday 20th June, 2006

                    J Offline
                    J Offline
                    Jorgen Sigvardsson
                    wrote on last edited by
                    #9

                    Viorel. wrote:

                    The second one does not allow this, but is little faster because of missing return value.

                    You'll save 1, or maybe 2 CPU cycles. (Depending on how the compiler allocates registers, you may end up saving no cycles)

                    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