class A
{
A a;//why can't we do this in C++ but it is allowed in Java?
};
Using this question, i came to know why it is not allowed in C++ but why is it allowed in java? According to same argument of not knowing the memory size while compilation, it should not be allowed in Java as well.
In Java, if you have
A a;,ais a reference to an object of classA. In C++, it’s not a reference, it’s a complete object.So the resulting object in C++ would be of infinite size, which isn’t very practical.