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. What's the difference?

What's the difference?

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

    class Integer {
    int i;
    public:
    Integer(int ii) : i(ii) {}
    const Integer operator+(const Integer& rv) const
    {
    cout << "operator+" << endl;
    return Integer(i + rv.i);
    }
    };

    Regarding the codes above what's the difference between

    ...
    return Integer(i + rv.i);
    ...

    and

    ...
    Interger a(i + rv.i);
    return a;
    ...

    In addition in what context should one of these syntax be used?


    T G T 3 Replies Last reply
    0
    • L LiYS

      class Integer {
      int i;
      public:
      Integer(int ii) : i(ii) {}
      const Integer operator+(const Integer& rv) const
      {
      cout << "operator+" << endl;
      return Integer(i + rv.i);
      }
      };

      Regarding the codes above what's the difference between

      ...
      return Integer(i + rv.i);
      ...

      and

      ...
      Interger a(i + rv.i);
      return a;
      ...

      In addition in what context should one of these syntax be used?


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

      the only difference remains in the fact that in first last sample, you create a temporary variable, which only exists to return its content. it is so avoided if you want to optimize your code. the only way it could be useful is when debugging, by setting a breakpoint at this point, you can control how the temporary variable is created. that's all.


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      L 1 Reply Last reply
      0
      • T toxcct

        the only difference remains in the fact that in first last sample, you create a temporary variable, which only exists to return its content. it is so avoided if you want to optimize your code. the only way it could be useful is when debugging, by setting a breakpoint at this point, you can control how the temporary variable is created. that's all.


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        L Offline
        L Offline
        LiYS
        wrote on last edited by
        #3

        I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!

        ...
        Interger a(i + rv.i);
        return a;
        ...

        Its output was constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)

        ...
        return Integer(i + rv.i);
        ...

        Its output was constructor -> destructor I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???


        B T 2 Replies Last reply
        0
        • L LiYS

          I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!

          ...
          Interger a(i + rv.i);
          return a;
          ...

          Its output was constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)

          ...
          return Integer(i + rv.i);
          ...

          Its output was constructor -> destructor I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???


          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          In the first case, the compiler creates a temporary object on the stack (in the function, object a). You return this object by value, so the compiler creates another temporary object on the stack (the return value) and then calls the copy constructor to copy object a into the temporary object. Than, object a gets destroyed and after finishing the statement, the temporary return value gets destroyed also. In the second case, you introduce no temoray variable in the function itself. You call the contructor to return the value. In this case, the destructor of the returned temporary variable is called. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          T 1 Reply Last reply
          0
          • B Bob Stanneveld

            In the first case, the compiler creates a temporary object on the stack (in the function, object a). You return this object by value, so the compiler creates another temporary object on the stack (the return value) and then calls the copy constructor to copy object a into the temporary object. Than, object a gets destroyed and after finishing the statement, the temporary return value gets destroyed also. In the second case, you introduce no temoray variable in the function itself. You call the contructor to return the value. In this case, the destructor of the returned temporary variable is called. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

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

            Bob Stanneveld wrote: In the second case, you introduce no temoray variable in the function itself. in fact, it is done implicitely, but a tempoary object is still created...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            1 Reply Last reply
            0
            • L LiYS

              I modified the forward example a lit bit. In which constructor, copy-constructor and destructor announce themselves!

              ...
              Interger a(i + rv.i);
              return a;
              ...

              Its output was constructor -> copy-constructor(cuz return by value) -> destructor(local object goes out of scope) -> destructor(temp object generated by compiler which was to hold returned object got destructed)

              ...
              return Integer(i + rv.i);
              ...

              Its output was constructor -> destructor I pretty clear with the former's process and obviously it has a much more overhead compare to the latter, but I wonder since there's no CC calling in the former, how can I get the returned value?What exactly the process the former is taking???


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

              by making return Integer(i + rv.i); the compiler creates a temporary object which is initalized by its constructor (called here with the parameter i + rv.i). then, by executing return the program exits the local scope of the function and so calls the destructors of the local variables... nothing more, nothing less.


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              1 Reply Last reply
              0
              • L LiYS

                class Integer {
                int i;
                public:
                Integer(int ii) : i(ii) {}
                const Integer operator+(const Integer& rv) const
                {
                cout << "operator+" << endl;
                return Integer(i + rv.i);
                }
                };

                Regarding the codes above what's the difference between

                ...
                return Integer(i + rv.i);
                ...

                and

                ...
                Interger a(i + rv.i);
                return a;
                ...

                In addition in what context should one of these syntax be used?


                G Offline
                G Offline
                guypremont
                wrote on last edited by
                #7

                Actually, there is no real diference. Do whatever is clearer to you.... Any decent compiler is intelligent enough to optimise away the temporary variables. The compiler does a much better job at low-level optimisation than any programmer, I usually leave this job to it and keep the code the clearest for me. My 2 cents.. Guy :)

                1 Reply Last reply
                0
                • L LiYS

                  class Integer {
                  int i;
                  public:
                  Integer(int ii) : i(ii) {}
                  const Integer operator+(const Integer& rv) const
                  {
                  cout << "operator+" << endl;
                  return Integer(i + rv.i);
                  }
                  };

                  Regarding the codes above what's the difference between

                  ...
                  return Integer(i + rv.i);
                  ...

                  and

                  ...
                  Interger a(i + rv.i);
                  return a;
                  ...

                  In addition in what context should one of these syntax be used?


                  T Offline
                  T Offline
                  TheGreatAndPowerfulOz
                  wrote on last edited by
                  #8

                  no real difference. But depending on the return type of the function, the second bit of code could create another temporary. For instance: int function() { return Integer(i + rv.i); } and int function() { Integer a(i + rv.i); return a; } will both create a temporary object (even 'a' could be considered temporary) and return the encapsulated int value. While Integer function() { return Integer(i + rv.i); } and Integer function() { Integer a(i + rv.i); return a; } Will both create TWO temporary objects, one inside the function, and one as the return value.

                  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