Line 11 above declares a variable:
There is the "normal" declaration
myClass s1 ;
, name "s1", type "myClass".
There are also values for initialization
(5, 6, 2)
Therefore, the constructor
myClass(int, int, int)
is automatically called during that declaration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
using namespace std;
class myClass {
public:
int a {}, b {}, c {};
myClass(int A, int B, int C) : a(A), b(B), c(C) {}
};
void print( myClass x ) {
cout << x.a << endl << x.b << endl << x.c;
}
int main() {
myClass s1(5, 6, 2);
print( s1 );
}
|
This version has two calls to constructors of myClass.
On line 15, when s1 is declared
and
on line 16. The function call takes myClass object
by value parameter.
Effectively, that function call declares
myClass x( s1 );
That constructor is not the
myClass(int, int, int)
. Compiler has automatically created another constructor for myClass:
myClass( const myClass& )
That is known as
copy constructor.