Home > Software engineering >  When object is a argument of function, why does function use copy constructor?
When object is a argument of function, why does function use copy constructor?

Time:03-11

I'm studying about copy constructors now. I learned that copy constructor is called when we make a object with already made object. And I heard that When we use object as a argument in function, copy constructor is called.

I want to know what happens inside function. How can function knows that function have to use copy constructor? I think inside function, the passed argument is assigned to function parameter so that copy constructor is called.

CodePudding user response:

I think inside function, the passed argument is assigned to function parameter

No. The arguments are essentially local variables of the called function, except they are created by the caller before the execution goes into the function. The function does not even know how they were constructed.

why does function use copy constructor?

So this assumption is wrong, the function does not use copy constructor on its arguments. The caller does (if argument is a variable and passed by value, so copy needs to be made).

CodePudding user response:

How can function knows that function have to use copy constructor?

When passing the argument by value, the parameter is initialized by the passed argument. And from copy constructor's documentation:

The copy constructor is called whenever an object is initialized (by direct-initialization or copy-initialization) from another object of the same type (unless overload resolution selects a better match or the call is elided), which includes :

  • function argument passing: f(a);, where a is of type T and f is void f(T t);

So, since the passed argument is used to initialize the parameter(in case of pass by value), according to the above quoted statement, the copy constructor will be used.


Do note that in C , assignment and initialization are different.


Additionally, note that move constructor can also be used instead of copy constructor in case the argument is an rvalue. From move constructor's documentation:

The move constructor is typically called when an object is initialized (by direct-initialization or copy-initialization) from rvalue (xvalue or prvalue) (until C 17)xvalue (since C 17) of the same type, including

  • function argument passing: f(std::move(a));, where a is of type T and f is void f(T t);

CodePudding user response:

First of all, it's inaccurate to say that a copy constructor is called.

Let me give some examples:

void foo(string b);
...
{
    string a = "asdf";
    foo(a); // copy constructor is called to make string b
    foo(std::move(a)); // move constructor is called to make string b
    foo("asdfg"); // the string b is constructed inplace from "asdfg"
}

Basically, foo has the string as a local parameter and it needs to be constructed in some way. How exactly it is constructed is defined by the call.

  •  Tags:  
  • c
  • Related