码迷,mamicode.com
首页 > 编程语言 > 详细

<Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<二>

时间:2015-04-13 12:42:59      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

<Item 9> Never call virtual functions during construction or destruction

1、you shouldn‘t call virtual functions during construction or destruction, because the calls won‘t do what you think, and if they did, you‘d still be unhappy. If you‘re a recovering Java or C# programmer, pay close attention to this Item, because this is a place where those languages zig, while C++ zags.java可以在构造函数中调用子类函数,但是子类成员变量此时没有初始化。

2、During base class construction, virtual functions never go down into derived classes. Instead, the object behaves as if it were of the base type. Informally speaking, during base class construction, virtual functions aren‘t.

3、It‘s actually more fundamental than that. During base class construction of a derived class object, the type of the object is that of the base class. Not only do virtual functions resolve to the base class, but the parts of the language using runtime type information (e.g., dynamic_cast (see Item 27) and typeid) treat the object as a base class type. An object doesn‘t become a derived class object until execution of a derived class constructor begins.

Upon entry to the base class destructor, the object becomes a base class object, and all parts of C++ — virtual functions, dynamic_casts, etc., — treat it that way.

 4、不小心在构造函数或者析构函数中间接调用虚函数是C++的常见错误,因此最好在编码规范中严格进行限制。

class Transaction {

public:
  Transaction()
  { init(); }                                      // call to non-virtual...

  virtual void logTransaction() const = 0;
  ...

private:

  void init()
  {
    ...
    logTransaction();                              // ...that calls a virtual!
  }
};

 

 可以在派生类中定义必要的static函数向基类构造函数传递必要的信息

class Transaction {

public:
  explicit Transaction(const std::string& logInfo);
  void logTransaction(const std::string& logInfo) const;   // now a non-
                                                           // virtual func
  ...
};

Transaction::Transaction(const std::string& logInfo)
{
  ...
  logTransaction(logInfo);                                // now a non-
}                                                         // virtual call

class BuyTransaction: public Transaction {
public: BuyTransaction( parameters ) : Transaction(createLogString( parameters )) // pass log info { ... } // to base class ... // constructor
private: static std::string createLogString( parameters ); };

 

 Using a helper function to create a value to pass to a base class constructor is often more convenient (and more readable) that going through contortions in the member initialization list to give the base class what it needs.

5、Things to Remember

  • Don‘t call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor.

 

<Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<二>

标签:

原文地址:http://www.cnblogs.com/GloryLion/p/4421647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!