Home > Net >  Exactly how are parameters initialized by the arguments passed in the function call in C ?
Exactly how are parameters initialized by the arguments passed in the function call in C ?

Time:05-10

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.

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related