I understand that to initialize a reference member variable in a class i have to use initializers list using a parametrized constructor to initialize the member variable. Also i understand that there is no need of a default constructor.
Question:
But what i don't understand is why defining a default constructor throws compilation error? Isn't compiler smart enough to consider parametrized constructor in the following case instead of throwing error?
If at all there is convincing answer for the above question then why would the compiler allow declaration of default constructor(without definition)? what difference does it make here?
:
class SomeClass
{
public:
//SomeClass(){} //THIS IS AN ISSUE
//SomeClass(); //THIS IS FINE THOUGH
SomeClass(int j):i(j){}
int& i;
};
int main()
{
SomeClass obj(2);
return 0;
}
I believe the same is the behavior with const member functions too. Is the reason same for reference and const member variables both?