Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ reference pointing to a temporary variable?

Tags:

c++

class ClassB {
    int m_b;
public:
    ClassB(int b) : m_b(b) {}
    void PrintClassB() const {
        cout << "m_b: " << m_b << endl;
    }
};


int main(int argc, char* argv[])
{
    const ClassB &af = ClassB(1);
    af.PrintClassB(); // print m_b: 1 with vs2008 & gcc 4.4.3
}

Given the above code, I have difficulties to understand this snippet:

Q1> What does this line mean?

const ClassB &af = ClassB(1);

Here is my understanding:

af refers to a temporary variable ClassB(1) and after the

execution of this line, the temporary variable is destroyed and af refers to an undefined variable. During this procedure, no copy-constructor is called.

Then why we can still issue the following statement and obtain the results?

af.PrintClassB(); // print m_b: 1 with vs2008 & gcc 4.4.3
like image 917
q0987 Avatar asked Aug 12 '11 05:08

q0987


People also ask

What is temporary variable in C?

In computer programming, a temporary variable is a variable with short lifetime, usually to hold data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short-lived, it is usually declared as a local variable, i.e., a variable with local scope.

How do you declare a temp variable in C++?

From C++ 17 you can declare temporary variables on if and switch statements just like loops. As the variables are temporary, you can't access those variables as they are only declared in the if or switch block.

What is the use of reference variable?

A reference variable provides a new name to an existing variable. It is dereferenced implicitly and does not need the dereferencing operator * to retrieve the value referenced. On the other hand, a pointer variable stores an address. You can change the address value stored in a pointer.

When to use a reference c++?

Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don't need “reseating”. This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.


2 Answers

const ClassB &af = ClassB(1);

const here extend the life time of the temporary object (i.e., ClassB(1)) being created. It's scope lasts until af falls out of scope;

af.PrintClassB(); // print m_b: 1 with vs2008 & gcc 4.4.3

This is because, af is nothing but the temporary object's reference which was constructed passing 1 to it's constructor.

like image 113
Mahesh Avatar answered Oct 26 '22 00:10

Mahesh


What you see is guaranteed by the Standard.

C++03 12.2 Temporary objects:

The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below...

[Example:

class C {
    // ...
public:
    C();
    C(int);
    friend C operator+(const C&, const C&);
    ˜C();
};
C obj1;
const C& cr = C(16)+C(23);
C obj2;

..a third temporary T3 to hold the result of the addition of these two expressions. 
The temporary T3 is then bound to the reference cr..The temporary T3 bound to the
reference cr is destroyed at the end of cr’s lifetime, that is, at the end of the
program. 

—end example]

like image 42
Eric Z Avatar answered Oct 25 '22 23:10

Eric Z