In the constructor
mySecondClass(myClass& reference)
{
myClassReference = reference; //doesn't get accepted by the compiler
};
there is used reference myClassReference
that shall be initialized when it is created. However the variable was not initialized. There is used the copy assignment operator.
In this constructor
mySecondClass(myClass& reference) : myObjectReference(reference){}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the reference is created and initialized in the mem-initializer list.
To make it more clear consider the following demonstrative program.
#include <iostream>
struct Int
{
Int() { std::cout << "Int()\n"; }
Int( int x ) : x( x ) { std::cout << "Int( " << x << " )\n"; }
Int & operator =( int x )
{
std::cout << "Int & operator =( " << x << " )\n";
this->x = x;
return *this;
}
int x;
};
struct A
{
A( int x )
{
this->value = x;
}
Int value;
};
struct B
{
B( int x ) : value( x ) {}
Int value;
};
int main()
{
A a( 10 );
std::cout << '\n';
B b( 10 );
return 0;
}
Its output is
Int()
Int & operator =( 10 )
Int( 10 )
That is when the body of the constructor A gets the control the data member was already created using the default constructor of the class Int. Within the body of the constructor there is used the assignment operator.
Opposite to the constructor of the class A the constructor of the class B created the data member in the mem-inkitializer list using the constructor of the class Int with a parameter.
So now imagine that you are using a reference in the class A. Then it is "default-initialized" that is it is not actually initialized by any valid object to which it shell refer. So in the body of the constructor you are trying to assign a value to an invalid reference that refers nowhere. So the compiler issues an error.