So, what errors actually exist in this code:
Well, you can start by saying that the code will not run at all at the compilation stage! The main function (main) returns nothing (
void main() ), but must return "int" => (
int main() ).
Error in Bar constructor.
To build an object, the base class constructor must be called. Since it is not specifically stated which constructor should be called, the compiler tries to substitute the default constructor call
"Foo::Foo()" , but this is not defined in the class
In the constructors of both classes there is no check for the negative of the variable
"j" , as a result of which the constructor will try to create an array with a negative dimension, and this is a disaster ...
Fourth (memory leaks):
There are two (!) Memory leaks in this code! The first is obvious, the pointer is copied. But the second leak is hidden in the call
"delete b" . This call will remove the object
*b , but will only cause the destructor of the Foo-parts of the object
*b , since the destructor is not virtual. Result: re-freeing the memory
Conclusion: a solid hole, not a code ... an error in almost every line.
Тестировалось в компиляторе gcc 4.6 .