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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:16:46+00:00 2026-06-05T10:16:46+00:00

I guess there is something about temporary objects that I don’t understand. Given the

  • 0

I guess there is something about temporary objects that I don’t understand.
Given the relationships:

class C {};

class F {
public:
    C getC() { return C(); };
};

class N {
public:
    N( C & base ){};
};

This works:

N n(C());

This doesn’t work:

F f;
N n(f.getC()); //compile error

Why?

  • 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-06-05T10:16:48+00:00Added an answer on June 5, 2026 at 10:16 am

    A non-const reference (like B& base) can only bind to an lvalue.

    F::getC() returns a C object by value, so the call expression f.getC() is an rvalue, not an lvalue.


    The reason that N n(C()); works, however, is due to an unrelated problem.

    This does not declare an object. It declares a function named n that returns N and takes a parameter of type “pointer to a function that has no parameters and returns C.”

    This is one manifestation of a language peculiarity known as the most vexing parse. To change this to declare an object, you’d need one of the following:

    N n = C();  // Use copy initialization
    N n((C())); // Use more parentheses
    

    Both of these would fail to compile, though, because both would attempt to bind the result of the rvalue expression C() to the non-const reference B& base.


    A const reference (like B const& base) can bind to an rvalue, as can an rvalue reference (like B&& base) in C++11.

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

Sidebar

Related Questions

There's something very unsatisfactory about this code: /* Given a command string in which
Are there any open source (or I guess commercial) packages that you can plug
I guess another way to phrase this would be Is there a class like
I have this homework that I gotta finish, but then something about it is
I want to make sure that a given group of objects is immutable. I
There must be something fundamental about interfaces/generics I have not yet learned. I hope
I guess there should be some event for this, but didn't find any. The
I guess there will be a very simple answer to this. But here goes.
I guess there must be an auto reindex every time a delete takes place.
I guess there might be some overlapping with previous SO questions, but I could

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.