I had a nasty experience with C++ initialization, and I'm trying to see whether there is a real-world example which justifies no warnings from the compiler.
The following code compiles correctly, but foo and bar get initialized with uninit values (I assume from the uninitialized parent class). The compilers, both g++ and VS, don't emit any warnings. I have been told of course that it is bad behavior to leave members public and don't decorate them. However, I assume the compiler could spot this kind of inconsistency and at least issue a warning at the highest warning levels, because I can't see any application of this kind of code.
#include <iostream>
using namespace std;
class base_class {
public:
int foo;
int bar;
base_class(int foo,int bar):
foo(foo),bar(bar)
{}
};
class derived_class: public base_class {
public:
derived_class(int Foo, int Bar):
base_class(foo,bar)
{
int a = Foo * Bar;
a++;
cout << foo << " " << bar << endl;
}
};
int main ()
{
derived_class *buzz = new derived_class(1,2);
buzz->print();
}