I'll rewrite the function for you.
1 2 3 4
|
A::A(const int &a)
: a(a)
{
}
|
This is a constructor for class
A
that take an int by const reference as you said.
: a(a)
is an initializer. The first a is the member
a
in the class, the second a is the parameter
a
.
The function body is empty.
Let's change your example a little.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class B
{
std::string s;
public:
B(const std::string& str) :
s(str)
{
}
B(const std::string& str, int num)
{
s = str;
}
};
|
Here we have two constructors. I've used an overload to distinguish them, but they demonstrate two different ways of initialising member
s
.
The first constructor that uses an initialiser list calls the copy constructor on
std::string
.
The second constructor calls the default constructor on
std::string
to intialize
s
, then it calls the assignment operator to copy the content of
str
into
s
.
I hope you can see that the first constructor is more efficient.