In more efficient C ++, the interesting point is that mixed array and polymorphism is a bad idea. For instance,
class Base { public: Base(int y) : a(y) {} int a; }; class D : public Base { public: D(int w, int y) : Base(y), c(w) {} int c; }; std::ostream& operator<<(std::ostream& os, const Base &obj ) { os << obj.a << std::endl; return os; }
Note. I know this is a bad design, but to illustrate the point.
The problem is that when mixing these two methods, which I have above, when we iterate the array for printing, we will not advance the array element by the correct amount (i.e. we move to sizeof(Base) instead of sizeof(D) ) This leads to the conclusion:
10 0 11 1 12
[Real-time example.]
(And I suppose the call to operator<< , as this is probably UB).
When compiling with g++ -std=c++1y -Wall -Weffc++ -pedantic main.cpp I do not receive any warnings or errors.
- Is there a compiler flag that I can enable that indicates a warning in this scenario?
- If not, why not?
source share