Use rules for constructor initialization table

xiaoxiao2021-03-06  36

Class a {a (int x); // a constructor}; Class B {B (int X, int y); // B constructor}; b :: b (int x, int y): a (x) // The constructor {} in the initialization table is called: (1) If there is a inheritance relationship, the derived class must call the constructor of the base class in its initialization table. (2) The CONST constant of the class can only be initialized in the initialization table because it cannot be initialized by the assignment in the function body. Class a {a (int size); const int size;}; A :: a (int size): initialization of data members of the size (size) {} (3) class can adopt two ways to assign value in the initialization table or function The efficiency of these two methods is not exactly the same. Members of non-internal function data types should be initialized in the first way to obtain higher efficiency. Class a {a (void); // No parameter constructor; // copy constructor A & Operator = (const a & otherf) // assignment function}; Class B {public: B (Const A & a); // B constructor private: a m_a; // member object}; b :: b (const A & a): m_a (a) {} For internal data type data, two initialization methods The efficiency is almost no different, but the latter's layout is clearer. Class f {public: f (int x, int y); // Constructor private: int m_x; int m_y;}; f :: f (int x, int y) {m_x = 0; m_y = 0;}

转载请注明原文地址:https://www.9cbs.com/read-75361.html

New Post(0)