(I come from C world and I am a beginner in C++, so simply answer the question)
In c++, argument are passed by value. So I try the following code to understand how it works.
#include <iostream>
using namespace std;
class MyClass {
int a;
public:
MyClass() {
a = 0;
cout<<"Default Constructor call\n"; }
MyClass(int x) {
a = x;
cout<<"Constructor call\n"; }
~MyClass() {
cout<<"Destructor call\n"; }
};
void myfoo(MyClass arg) {}
int main() {
cout<<"Obj declaration\n";
MyClass obj(10);
cout<<"Function call\n";
myfoo(obj);
cout<<"End of main\n";
}
The destructor is called at the end of the function and the constructor is not called at the beginning because there is no constructor MyClass(MyClass& xxx).
So, how the object arg is constructed in the function ? A simple memory copy ? Is it enough or is it better to always have a constructor MyClass(MyClass& xxx)