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. function header temporary variable

function header temporary variable

Scheduled Pinned Locked Moved C / C++ / MFC
c++
3 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.
  • L Offline
    L Offline
    LionAM
    wrote on last edited by
    #1

    Hello! Is it part of the C++ standard that a temporary variable instantiated in the function header is destroyed after a possible assignment operation, so that the following code can be used safely:

    class Obj
    {
    //...
    };

    Obj& function(Obj& temp = Obj())
    {
    return temp;
    }

    int main()
    {
    Obj myObj;
    myObj = function();

    //...
    
    return 0;
    

    }

    In VC++ 2008, this works as expected. In contrast, when the object is instantiated in the function body, it is destroyed before the assignment operator - so this code is not valid:

    Obj& function()
    {
    Obj temp = Obj();
    return temp;
    }

    Thank you in advance. Alex

    B 1 Reply Last reply
    0
    • L LionAM

      Hello! Is it part of the C++ standard that a temporary variable instantiated in the function header is destroyed after a possible assignment operation, so that the following code can be used safely:

      class Obj
      {
      //...
      };

      Obj& function(Obj& temp = Obj())
      {
      return temp;
      }

      int main()
      {
      Obj myObj;
      myObj = function();

      //...
      
      return 0;
      

      }

      In VC++ 2008, this works as expected. In contrast, when the object is instantiated in the function body, it is destroyed before the assignment operator - so this code is not valid:

      Obj& function()
      {
      Obj temp = Obj();
      return temp;
      }

      Thank you in advance. Alex

      B Offline
      B Offline
      barneyman
      wrote on last edited by
      #2

      In the first example you're returning a reference to an object that exists *outside* the function (it was created by the caller, *before* it called the function) In the second you're returning a reference to something that no longer exists

      L 1 Reply Last reply
      0
      • B barneyman

        In the first example you're returning a reference to an object that exists *outside* the function (it was created by the caller, *before* it called the function) In the second you're returning a reference to something that no longer exists

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

        Thank you, then it is clear! Alex

        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