在构造函数中调用virtual函数时,base class构造期间virtual函数是不会下降到derived class层
如:
class Transaction{
public:
Transaction();
virtual void logTransaction() const = 0;
};
Transaction::Transaction()
{
logTransaction();
}
class BuyTransaction: public Transaction{
public:
virtual void logTransaction() const;
};
BuyTransaction b;
但执行b时,会先构造Transaction,但是构造函数中不会调用BuyTransaction的logTransaction
时间: 2024-10-12 14:59:38