hjk41的日志

Avatar

Effective C++ 48: Pay attention to compiler warnings

Unlike compilers of other languages, C++ compilers warns you only when it thinks there is something wrong. For example, here is a mistake everyone makes at one time or another.

class B {
public:
  virtual void f() const;
};

class D: public B {
public:
  virtual void f();
};


As was disscussed before, functions in C++ can be overloaded by using const. So the function B::f() will be overloaded by D::f(), meaning that there will be no polymorphism for D::f(), which is apparently not our intention here. And most C++ compilers will generate a warning about that. If you just ignore the warning, you will have to spend a lot of time before you can track the problem down.

评论已关闭