<effective c++> Reading Digest--ctors, Dtors and assignment operators< two >

Source: Internet
Author: User

<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 does what you think, and If they did, you ' d still is unhappy. If you ' re a recovering Java or C # Programmer, pay close attention to the this Item, because the is a place where those Langua Ges Zig, while C + + Zags.java can call a subclass function in a constructor, but the child class member variable is not initialized at this time.

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. During base class construction of a derived class object, the type of the object is that of the base class. Not only does virtual functions resolve to the base class, but the parts of the language using runtime type information (e.g ., dynamic_cast (see Item) 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.

4, accidentally calling virtual functions indirectly in a constructor or destructor is a common error in C + +, so it is best to restrict it strictly in the coding specification.

class Transaction {public:  Transaction ()  {init ();}                                       // Call to non-virtual  ... Virtual void Const 0 ;  ... Private :   void Init ()  {    ...    Logtransaction ();                               // ... that calls a virtual!   }};

You can define the necessary static functions in a derived class to pass the necessary information to the base class constructor

classTransaction { Public:  ExplicitTransaction (ConstSTD::string&loginfo); voidLogtransaction (ConstSTD::string& Loginfo)Const;//Now a non-//virtual func  ...}; Transaction::transaction (ConstSTD::string&loginfo)                                {... logtransaction (loginfo); //Now a non-}                                                         //Virtual CallclassBuytransaction: PublicTransaction {
Public: buytransaction (Parameters): Transaction (createlogstring (parameters))//Pass Log Info { ... } //To base class ... //Constructor
Private: StaticSTD::stringcreatelogstring (parameters);};

Using a helper function to create a value to pass to a base class constructor are often more convenient (and more readable) That's 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 would never go to a more derived class than that's the currently executing constructor or destructor.

<effective c++> Reading Digest--ctors, Dtors and assignment operators< two >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.