wangxf
wangxf

Reputation: 160

I do not understand somethings in 'Argument Initialization' in <Inside The C++ Object Model>

In the book Chapter 2.2 Argument Initialization
When we call a function which have a class object argument, The compiler will construct a temp object by copy constructor, use the temp argument to call the function, and change the argument from class object to class reference, like this :

void foo( X x0 );   
X xx;   
foo(xx);  
------------------ 
X __temp0;  
__temp0.X::X(xx);   
void foo( X& x0 );  
foo(__temp0);

I don't understand why compiler should change the argument from class X object to class X reference?

Upvotes: 2

Views: 79

Answers (1)

StenSoft
StenSoft

Reputation: 9609

Note: Implementation details for x86-64 follow. Your architecture may vary.

This is because parameters are mostly passed in registers and there is not enough space to fit objects there. Therefore the compiler passes temporary references instead. It does not change the behavior at all and therefore is legal by the C++ standard.

Upvotes: 1

Related Questions