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

  • Home
  • SEARCH
  • 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 8075097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:52:42+00:00 2026-06-05T14:52:42+00:00

In Eckel, Vol 1, pg:367 //: C08:ConstReturnValues.cpp // Constant return by value // Result

  • 0

In Eckel, Vol 1, pg:367

//: C08:ConstReturnValues.cpp
// Constant return by value
// Result cannot be used as an lvalue
class X {
   int i;
public:
   X(int ii = 0);
   void modify();
};

X::X(int ii) { i = ii; }

void X::modify() { i++; }

X f5() {
   return X();
}

const X f6() {
   return X();
}

void f7(X& x) { // Pass by non-const reference
   x.modify();
}

int main() {
   f5() = X(1); // OK -- non-const return value
   f5().modify(); // OK
// Causes compile-time errors:
//! f7(f5());
//! f6() = X(1);
//! f6().modify();
//! f7(f6());
} ///:~

Why does f5() = X(1) succed? What is going on here???

Q1. When he does X(1) – what is going on here? Is this a constructor call –
shouldn’t this then read X::X(1); Is it class instantiation – isn’t class
instantiation something like: X a(1); How does the compiler determine what
X(1) is?? I mean.. name decoration takes place so.. X(1) the constructor
call would translate to something like: globalScope_X_int as the function
name.. ???

Q2. Surely a temporary object is used to store the resulting object that X(1)
creates and then would’t that be then assigned to the object f5() returns
(which would also be a temporary object)? Given that f5() returns a temporary
object that will be soon be discarded, how can he assign one constant temporary
to another constant temporary??? Could someone explain clearly why:
f7(f5()); should reult in a constant temporary and not plain old f5();

  • 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-05T14:52:44+00:00Added an answer on June 5, 2026 at 2:52 pm

    I wasn’t entirely satisfied by the answers, so I took a look at:

    “More Effective C++”, Scott Meyers. Item 19: “Understand the origin of
    temporary Objects”

    . Regarding Bruce Eckel’s coverage of “Temporaries”,
    well, as I suspect and as Christian Rau directly points out, it’s plain
    wrong! Grrr! He’s (Eckel’s) using us as guinea pigs!! (it would be a
    good book for newbies like me once he corrects all his mistakes)

    Meyer: “True temporary objects in C++ are invisible – they don’t
    appear in your source code. They arise whenever a non-heap object is
    created but not named. Such unnamed objects usually arise in one of
    two situations: when implicit type conversions are applied to make
    function calls succeed and when functions return objects.”

    “Consider first the case in which temporary objects are created to
    make function calls succeed. This happens when the type of object
    passed to a function is not the same as the type of the parameter to
    which it is being bound.”

    “These conversions occur only when passing objects by value or when
    passing to a reference-to-const parameter. They do not occur when
    passing an object to a reference-to-non-const parameter.”

    “The second set of circumstances under which temporary objects are
    created is when a function returns an object.”

    “Anytime you see a reference-to-const parameter, the possibility
    exists that a temporary will be created to bind to that parameter.
    Anytime you see a function returning an object, a temporary will be
    created (and later destroyed).”

    The other part of the answer is found in: “Meyer: Effective C++”, in
    the “Introduction”:

    “a copy constructor is used to initialize an object with a different
    object of the same type:”

    String s1;       // call default constructor
    String s2(s1);   // call copy constructor
    String s3 = s2;  // call copy constructor
    

    “Probably the most important use of the copy constructor is to define
    what it means to pass and return objects by value.”

    Regarding my questions:

    f5() = X(1) //what is happening?
    

    Here a new object isn’t being initialized, ergo this is not
    initialization(copy constructor): it’s an assignment (as
    Matthieu M pointed out).

    The temporaries are created because as per Meyer (top paragraphs),
    both functions return values, so temporary objects are being created.
    As Matthieu pointed out using pseudo-code, it becomes:
    __0.operator=(__1) and a bitwise copy takes place(done by the
    compiler).

    Regarding:

    void f7(X& x);
    f7(f5);
    

    ergo, a temporary cannot be created (Meyer: top paragraphs).
    If it had been declared: void f7(const X& x); then a temporary would
    have been created.

    Regarding a temporary object being a constant:

    Meyer says it (and Matthieu): “a temporary will be created to bind to that
    parameter.”

    So a temporary is only bound to a constant reference and is itself not
    a “const” object.

    Regarding:
    what is X(1)?

    Meyer, Item27, Effective C++ – 3e, he says:

    “C-style casts look like this: (T)expression //cast expression to be
    of type T

    Function-style casts use this syntax: T(expression) //cast expression
    to be of type T”

    So X(1) is a function-style cast. 1 the expression is being cast to
    type X.

    And Meyer says it again:

    “About the only time I use an old-style cast is when I want to call an
    explicit constructor to pass an object to a function. For example:

    class Widget {
      public:
        explicit Widget(int size);
        ...
    };
    
    void doSomeWork(const Widget& w);
    doSomeWork(Widget(15)); //create Widget from int
                            //with function-style cast
    
    doSomeWork(static_cast<Widget>(15));
    

    Somehow, deliberate object creation doesn’t “feel” like a cast, so I’d
    probably use the function-style cast instead of the static_cast in
    this case.”

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

Sidebar

Related Questions

I'm reading Eckel's book, IO chapter, and there is the following code (p. 667).
I'm reading Thinking in C++ by Bruce Eckel. In Chapter 15 (Volume 1) under
In 'Thinking in C++' by Bruce Eckel, there is a program given to print
I have the following class: class Stack { struct Link { void* data; Link*
Here is an example from Bruce Eckel Thinking in Java, 4th edition import java.io.BufferedReader;
I'm working on an example from Bruce Eckel's book and i was wondering why
I'm a little confused as to why I've been told to return const foo
I'm learning C++ right now using Bruce Eckel's Thinking in C++ and I'm in
I'm trying to compile gui/flex/songs.mxml in the fourth edition of Bruce Eckel's Thinking in
Consider the following piece of code. int var; cout << (long)&var; My doubt is

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.