Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7692423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:50:58+00:00 2026-05-31T20:50:58+00:00

What is the difference between these? MyType myFunction(); MyType t = myFunction(); AND MyType

  • 0

What is the difference between these?

MyType myFunction();

MyType t = myFunction();

AND

MyType &myFunction();

MyType t = myFunction();

AND

const MyType &myFunction();

MyType t = myFunction();

What is going on behind the scenes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:50:59+00:00Added an answer on May 31, 2026 at 8:50 pm

    In the three cases the second line is common:

    MyType t = myFunction();
    

    That line gets the result of calling myFunction and uses it to copy-construct a new element of MyType called t.

    Now on the differences. In the first case you are returning by value, which means that (semantically), the compiler will create a copy of the object that is in the return statement inside myFunction and then use that copy as the source for the copy construction of t. The compiler will most probably elide the copies (at least the second).

    In the other two cases, the functions return references to some other object. If the objects are locals, then it is Undefined Behavior. The difference between the two is whether the reference returned can be used to modify the referred object or not, and this might impact what copy constructor is used or if it can be used at all. Beware that the object from which you are obtaining the reference must outlive the function call, or you will be causing undefined behavior.

    // an example where it matters:
    typedef std::auto_ptr<int> MyType;
    MyType t = myFunction();
    

    Because std::auto_ptr modifies the right hand side on assignment, the previous code will only work if the returned reference is non-const.

    Luchian points out that returning a reference will most likely be undefined behavior, so when would it not be? When the object from which the reference was obtained outlives the uses of the reference. That is the basic building block of the Meyers singleton:

    // an example where returning a reference is correct
    MyType & myFunction() {
      static MyType instance;   // Note static storage duration!
      return instance;
    }
    

    Or any plain accessor that returns a reference to a subobject. Some of the common cases are operator[] in containers (they usually don’t copy the value, but return a reference to the stored data).

    But it is true that more often than not, functions don’t return statically lived objects, but rather locals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between these (in Const manner): const int getNum(int &a, int
What's the difference between these 2 lines CArray<MyClass, MyClass> MyArray CArray<MyClass, const MyClass &
Is there any difference between these tow pieces of code & which approach is
What's the difference between these two? Component.For<IMyType>() .LifeStyle .PerWebRequest .UsingFactoryMethod(() => new MyType()); and
What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass
What is the difference between these two pieces of code type IInterface1 = interface
What is the difference between these three heavily related fields? Is there one specific
1)Is there any difference between these two keywords for the elements of collections??( Copy
Can someone explain the difference between these two, the first one is taken from
Is there any difference between these two LINQ statements: var query = from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.