> cat nevercall.cpp
class Transaction {
public:
Transaction();
virtual void logTransaction() const = 0;
// ...
};
Transaction::Transaction()
{
//...
logTransaction();
}
class BuyTransaction: public Transaction {
public:
virtual void logTransaction() const;
//...
};
class SellTransaction: public Transaction {
public:
virtual void logTransaction() const;
// ...
};
int main()
{
BuyTransaction b;
}
当(且仅当)是pure时使用-Weffc++
警告,就像在 Meyer 的示例代码中一样:virtual void Transaction::logTransaction() const
> g++ -Weffc++ nevercall.cpp -o nevercall
nevercall.cpp:1:7: warning: 'class Transaction' has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]
nevercall.cpp: In constructor 'Transaction::Transaction()':
nevercall.cpp:12:20: warning: pure virtual 'virtual void Transaction::logTransaction() const' called from constructor [enabled by default]
nevercall.cpp: At global scope:
nevercall.cpp:15:7: warning: 'class BuyTransaction' has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]
nevercall.cpp:22:7: warning: 'class SellTransaction' has virtual functions and accessible non-virtual destructor [-Wnon-virtual-dtor]