Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exactly how are parameters initialized by the arguments passed in the function call in C++?

In the following code :

#include<iostream>
using namespace std;
void fun(T1 x, T2 y, T3 z){
    // some code
}
int main(){
   T1 a;
   T2 b;
   T3 c;
   fun(a,b,c);
}

I wanted to understand, how the parameters(x, y, and z) in the function "fun" are getting initialized by the arguments(a,b, and c) passed during the function call. Is the copy constructor invoked and then initialization happens like T1 x = a; T2 y = b; T3 z = c; or something else take place?

Please explain.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is the copy constructor invoked and then initialization happens like T1 x = a; T2 y = b; T3 z = c;

Yes, it happens exactly like this. Which, given the amount of different kinds of initializations in C++, is an impressive guess.

It’s called copy-initialization: (which in general doesn’t necessarily imply that a copy constructor is called)

[dcl.init.general]/14

The initialization that occurs in the = form of a brace-or-equal-initializer or condition ([stmt.select]), as well as in argument passing, function return, throwing an exception ([except.throw]), handling an exception ([except.handle]), and aggregate member initialization ([dcl.init.aggr]), is called copy-initialization.

"the = form of a brace-or-equal-initializer" refers to T1 x = a;, and "argument passing" speaks for itself.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading