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 4608814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:53:54+00:00 2026-05-22T00:53:54+00:00

Possible Duplicate: return type in c++ #include<iostream> int& fun(); int main() { int p=fun();

  • 0

Possible Duplicate:
return type in c++

#include<iostream>
int& fun();
int main()
{
  int p=fun();
  std::cout<<p;
  return 0;
}

int & fun()
{
  int a=10;
  return  &a;
}

why does this code give error as,error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int*'..Actually i am not clear about the temporaries i.e. when are they created and when are they get destroyed.?so ,please explain temporaries to some extent too.

  • 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-22T00:53:55+00:00Added an answer on May 22, 2026 at 12:53 am

    &a generates a temporary which cannot be bound to a non-const reference.

    Moreover your code has several flaws.

    1) &a has type int* whereas you are returning by reference i.e int &. The types don’t match.

    2) Even if you change &a to a in the return statement you code still won’t work because returning a local variable by reference and then using the result is UB.


    Invalid initialization of non-const reference from a temporary

    C++ doesn’t allow temporaries to be bound to non constant references.

    For example you can’t do something like this

    int &x = 5; 
    

    because the temporary int(5) would be destroyed at the end the expression which it is a part of. However references to const can be initialized from a temporary i.e you can safely write

    const int &x = 5;

    In this case attaching the temporary to a const-reference prolongs its lifetime. It gets destroyed when x gets destroyed.

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

Sidebar

Related Questions

Possible Duplicate: LINQ to SQL: Return anonymous type? This is my code: class B
Possible Duplicate: Java - why no return type based method overloading? The compiler does
Possible Duplicate: Why does Math.Floor(Double) return a value of type Double? Why does C#
Possible Duplicate: What does template <unsigned int N> mean? Hi ! Are non-type template
Possible Duplicate: Why does InputStream#read() return an int and not a byte? why return
Possible Duplicate: Does this type of memory get allocated on the heap or the
Possible Duplicate: LINQ to SQL: Return anonymous type? I have a standard LINQ to
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: trailing return type using decltype with a variadic template function I want
Possible Duplicate: Array of zero length I have seen such type of code :-

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.