"Depth exploration of C ++ Object Model" Chapter II: constructor semantics

Type conversion function operator:

operator 类型名() { //由类型名来确定返回值类型
    实现转换的语句
    ...
}

while (cin >> val) principle
cin is a stream object, stream object >> operator returns the left, that is to say to return cin cin >> Val, then while (cin >> val) becomes the while (cin ), the question becomes determine the legitimacy of a stream object in the statement.

operator void * () const; when the function is called while (cin) or if (cin), converting the object into a stream of type void *.
! bool operator () const; function is called while or if the time to convert into a stream object type bool (cin!) (cin!) .

#include<iostream>  
using namespace std;  
     
class A  
{  
    public:  
        A() {}  
        ~A() {}  
        operator void* () const
        {  
            cout << "cast to void*; ";  
            return (void *)this;  
        }  
        bool operator ! () const
        {  
            cout << "cast to bool; ";  
            return true;  
        }  
};  
     
int main()  
{  
    A a;  
    if (a) cout << "first" << endl;  
    if (!a) cout << "second" << endl;  
    return 0;  
}
//cast to void*; first
//cast to bool; second。

Conversion operator actually difficult to use in an anticipated good behavior. Its introduction is wise, but the test is harsh.

This leads to the implicit and explicit keywords

2.1 Default Constructor operating configuration

default constructors when needed is generated out of the compiler

The following four cases, cause "the compiler must synthesize a default for the classes of undeclared constructors constructor":

  1. member class object with the default constructor
  2. The base class with a default constructor
  3. With a virtual function of class
  4. With a virtual base class of the class

If you have a constructor that already exists, then the code will be placed compiler needs before your own code to do some initialization.

c ++ novice two common misconceptions:

  1. If you do not define any class default constructor, it will be a synthesized
  2. The compiler synthesized a default constructor displays the default value of each data member of the class set

2.2 copy constructor configuration operation

copy constructor arising out only when necessary compiler

The following four cases, a class does not show a bitwise copy semantics, that is, the compiler will synthesis for a default copy constructor:

  1. class containing a member object, the latter class declaration has a copy constructor (the designer can be displayed statement, the compiler may be synthesized)
  2. class inherits from a base class, which in the presence of a copy constructor (may also be synthesized statement displayed)
  3. class declares one or more virtual function (to set the correct vptr)
  4. derived from a class inheritance chaining, wherein one or more of virtual base classes (base class to set the correct offset value)

2.3 semantics transformation program

C ++ in strict terms, the "definition" means "take up memory" behavior

NRV Optimization: eliminating the need for local variables, reduces the construction cost of a copy
with the copy constructor, not the bitwise, slower, so the compiler will be optimized NRV

When using memcpy () or memset, there is to be noted that within the class containing vptr, vbtr

2.4 member initialization list (member initialization list)

In order to make the program compile correctly, about four cases, you must use the member initialization list:

  1. When initializing a reference member (c ++ 11 members at the time of the initial declaration can be set directly, without the need to function, the same below)
  2. When initializing a const member
  3. When you call a base class's constructor, but it has a set of parameters
  4. When you call a member class's constructor, but it has a set of parameters

The compiler will be placed in the appropriate order in the constructor initialization list in, and before the user code, placed the order list items in order to declare the decision of the members.


You can call a member function to set the initial value of a member, this method is legal, because when entering the constructor of this indicator has made it clear:

//X::xfool()被调用
X::X(int val) : i(xfoo(val)), j(val) {

}

Member of the subclass is used as the argument to the constructor of the parent class:

class FooBar : public X { 
 int _fval; 
public: 
 int fval() { return _fval; } 
 FooBar( int val ) 
 : _fval( val ), 
 X( fval() ) //先调用父类的构造函数,但是此时val还未设定初值,故结果不是想要的
 {} 
 ... 
};

Guess you like

Origin www.cnblogs.com/senshaw/p/10991116.html