C + + Skills regain

Source: Internet
Author: User

0, although the static member function does not exist the this pointer, but still can not declare in a class with the same parameter of the virtual function and static member functions.

1. A virtual function table in vftable is a pointer

2. Delete essence, call destructor to free memory at the same time
Object *o = new Object ();
Use Object
Delete o; Which first calls the destructor (if any) in its argument and then returns the memory allocated by new Store.
o = NULL;

3, ClassA *p = new ClassB;
Delete p; If the CLASSB does not have a destructor virtual function, the destructor of the ClassA is called
Think carefully, you can only invoke the destruction of the native a!

4. The base class part is constructed before the derived class part, and the data members in the derived class are not initialized when the base class constructor executes. If a virtual function call in a base class constructor is resolved to invoke a virtual function of a derived class, and the virtual function of the derived class accesses the uninitialized derived class data, it causes the program to have some undefined behavior and bugs.

5. A static or non-static function with the same name as the subclass overrides the parent class!

6, if a virtual function is inherited and not changed, then the virtual function pointer in the actual point to the parent class, egg pain, attention!!!

7, virtual function overload, the child class modifies the default parameter value of the parent class, then the value of the default parameter will be determined according to the type of the pointer itself

8, the role of the virtual keyword is to prompt the compiler after a late link, tell the connection process: "I am a virtual, do not connect me, and so on when the run."
1) A pre-linked or static binder: a function-linking is called a pre-or static-union at compile time.
2) late-post or dynamic-linking: a union that can be performed at run time is known as a late-post or dynamic-linked series.

9, the principle of virtual function: When the compiler encounters virtual, will construct a table and a pointer for the class, the table is called VTBL, each class has its own VTBL,VTBL function is to save the address of the virtual function in their own class, we can see the VTBL image as an array, Each element of this array holds the address of the virtual function. The pointer is called VPTR and points to that table. This pointer is stored in the corresponding object, which means that the address of the corresponding virtual function can be found only after the object has been created.
To ensure that the base class defined by the runtime and the virtual function of the derived class are not only the same as the function name, the return value and parameters must be the same, otherwise the system does not perform late-linking even if virtual is added.
10. The principle of operator overloading

In this case, operator overloading is pretty straightforward, and in fact operator overloading follows some principles:

Only existing C + + operators can be overloaded in 1.c++, and users are not allowed to define new operators themselves.

Most of the operators in 2.c++ can be overloaded, in addition to the member access operators., the scope operator::, the length operator sizeof, and the conditional operator?:.

3. Operator overloading cannot change the number of operands (operands) of an operator. For example: "+" is an operator that implements two operands, and is still a binocular operator after overloading.

4. Overloading cannot change the original precedence and the original binding of the operator.

6. Operator overloading cannot be all predefined basic data in C + +, and is intended to prevent users from modifying the nature of the operators used for basic type data.


11, the definition of overloaded functions: In the same declaration field the function name is the same, but the parameter table is different, that is, through the function's parameter table and uniquely identify and to distinguish the function of a special function.

12, in order to estimate which overloaded function is most suitable, you need to follow the following rules to judge:

Exact match: parameter matching without conversion, or just trivial conversions, such as array name to pointer, function name to pointer to function, t to const t;
Promotion matches: integer promotion (such as bool to int, char to int, short to int), float to double
Use standard conversion matching: such as int to double, double to int, double to long double, derived* to base*, t* to void*, int to unsigned int;
Use user-defined matches;
Use ellipsis to match: similar to the ellipsis parameter in printf
If multiple matching functions are found at the highest level, the call is rejected (because of ambiguity, ambiguous). Look at the following example:

void print (int);
void print (const char*);
void print (double);
void print (long);
void print (char);

void H (Char c,int i,short s, float f)
{
print (c);//exact match, call print (char)
print (i);//exact match, call print (int)
print (s);//integer promotion, call print (int)
Print (f);//float to double lift, call print (double)

Print (' a ');//exact match, call print (char)
Print (49);//exact match, call print (int)
Print (0);//exact match, call print (int)
Print ("a");//Exact match, call to print (const char*)
}

C + + Skills regain

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.