Thread: const reference to an anonymous object

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Not so far
    Posts
    9

    const reference to an anonymous object

    I was looking at some open source code whereby I came across this line:

    Code:
    const wxString &text{ GetLine(i) };
    Is this faster than assigning it to another variable directly, like:

    Code:
    const wxString text{ GetLine(i) };
    Or is there something else I'm overlooking?

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They have different semantics. First one creates a temporary and then creates a reference to it, extending the lifetime of the reference.
    The second creates a temporary, then invokes copy constructor to copy object into the new variable text, then destructs the temporary.
    The first one is possibly more efficient (not complex objects) if you don't need to modify the object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    The second creates a temporary, then invokes copy constructor to copy object into the new variable text, then destructs the temporary.
    The first one is possibly more efficient (not complex objects) if you don't need to modify the object.
    There could be return value optimisation though, in which case the second would be no less efficient than the first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Assuming that GetLine returns by value, there really isn't much efficiency difference, because it's easy for a compiler to see that you're doing the same thing.

    The second might be faster for global variables in rare cases, because in the first the type of the variable as seen by other compilation units is reference, which could potentially refer to another global variable, whereas in the second case it can't. So in that rare case it could prevent optimisations. But if it's found inside a function, then the initial value is visible to the optimizer so this is not a problem.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. force DCOM object authenticate as ANONYMOUS
    By tom_bali in forum Windows Programming
    Replies: 0
    Last Post: 08-14-2012, 11:57 AM
  2. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  3. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  4. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM

Tags for this Thread