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? Returning reference or value?

Which is better? Returning reference or value?

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpquestiondiscussion
5 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.
  • C Offline
    C Offline
    Code o mat
    wrote on last edited by
    #1

    Hey folks, i was just wondering, what do you think which of these would generate a faster way -if there's a difference at all- to query a class variable:

    //--- Method A ----
    class CMyClass
    {
    protected:
    int m_myVar;

    public:
    int GetVar() const { return m_myVar; }
    };

    //---- Method B ----
    ...
    const int &GetVar() const { return m_myVar; }
    ...

    //--- Method C ----
    ,,,
    inline int GetVar() const { return m_myVar; }
    ...

    //---- Method D ----
    ...
    inline const int &GetVar() const { return m_myVar; }
    ...

    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

    S D 2 Replies Last reply
    0
    • C Code o mat

      Hey folks, i was just wondering, what do you think which of these would generate a faster way -if there's a difference at all- to query a class variable:

      //--- Method A ----
      class CMyClass
      {
      protected:
      int m_myVar;

      public:
      int GetVar() const { return m_myVar; }
      };

      //---- Method B ----
      ...
      const int &GetVar() const { return m_myVar; }
      ...

      //--- Method C ----
      ,,,
      inline int GetVar() const { return m_myVar; }
      ...

      //---- Method D ----
      ...
      inline const int &GetVar() const { return m_myVar; }
      ...

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

      S Offline
      S Offline
      Sauro Viti
      wrote on last edited by
      #2

      In your example you are returning a basic datatype (an integer), then it's quite the same to return by reference or by value: for example on a 32 bit environment, returning by reference means load the address of m_myVar inside the eax register, while returning by value means load the content of m_myVar inside that register. The inlined versions of your methods could potentially be better, because the compiler could better optimize the usage of CPU registers and produce a code a bit more efficient. If your methods return a class or a struct the things could be different: returning by value means that your methods should allocate a temporary object on the stack and call its copy constructor. Then returning by reference in most of cases is better.

      C 1 Reply Last reply
      0
      • C Code o mat

        Hey folks, i was just wondering, what do you think which of these would generate a faster way -if there's a difference at all- to query a class variable:

        //--- Method A ----
        class CMyClass
        {
        protected:
        int m_myVar;

        public:
        int GetVar() const { return m_myVar; }
        };

        //---- Method B ----
        ...
        const int &GetVar() const { return m_myVar; }
        ...

        //--- Method C ----
        ,,,
        inline int GetVar() const { return m_myVar; }
        ...

        //---- Method D ----
        ...
        inline const int &GetVar() const { return m_myVar; }
        ...

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

        D Offline
        D Offline
        Dr Walt Fair PE
        wrote on last edited by
        #3

        Actually in the long run, return by value may be shorter, but it depends on what you are measuring. For basic data types, the actual return doesn't make much difference, but then when you go to use the returned value, if it is returned by reference, there is an extra level of indirection to resolve. Of course what the compiler optimizes out and what else you might do with the results enters into the overall performance, so it's not a simple matter to determine a priori. However, in most cases, I doubt the difference is major.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        C 1 Reply Last reply
        0
        • S Sauro Viti

          In your example you are returning a basic datatype (an integer), then it's quite the same to return by reference or by value: for example on a 32 bit environment, returning by reference means load the address of m_myVar inside the eax register, while returning by value means load the content of m_myVar inside that register. The inlined versions of your methods could potentially be better, because the compiler could better optimize the usage of CPU registers and produce a code a bit more efficient. If your methods return a class or a struct the things could be different: returning by value means that your methods should allocate a temporary object on the stack and call its copy constructor. Then returning by reference in most of cases is better.

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Of course with more complex types it's usually better to go by reference, i was just wondering about the basic types indeed. Thank you for your answer. :thumbsup:

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

          1 Reply Last reply
          0
          • D Dr Walt Fair PE

            Actually in the long run, return by value may be shorter, but it depends on what you are measuring. For basic data types, the actual return doesn't make much difference, but then when you go to use the returned value, if it is returned by reference, there is an extra level of indirection to resolve. Of course what the compiler optimizes out and what else you might do with the results enters into the overall performance, so it's not a simple matter to determine a priori. However, in most cases, I doubt the difference is major.

            CQ de W5ALT

            Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #5

            Thanks for the info.

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

            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