C language and C ++ considerations)

Source: Internet
Author: User

1. The macro definition cannot wrap a line. To wrap a line, use a line break \

Operator overload:

Conclusion 1: For binary operators, it is easier to overload a friend function than to overload a member function. A binary operator does not require the first parameter to be a certain type of object.

Conclusion 2: For The unary operator, reload it into a member function is the most appropriate. You can also use the overload function as a Member.

TIPS: alias for pointer Variables

Char A = 'M ';
Char * P = &;
Char * & pr = P; (the alias is also a pointer first, so char * is followed by the reference symbol, so char * & pr = P;

And the alias char ** & pr = P for the pointer derived from this algorithm;

Differences between array name and array name obtaining address:

Set int s [5] = {1, 2, 3, 4, 5}; then s is the first address of the array (meaning the address of the first element of the array); & S is the address of the array. Although the values are the same, their meanings are different. We can know their deep meanings from S + 1 and & S + 1.

{<Br/> int num [5] = {1, 2, 3, 4, 5}; <br/> cout <num <Endl; <br/> cout <num + 1 <Endl; <br/> cout <& num <Endl; <br/> cout <& num + 1 <Endl; <br/>}< br/>

Constructor features::
(1) constructor is a member function. The function body can be written in the class body or in the class body.
(2) constructor is a special function with the same name as the class name. This function does not specify the type description. It has an implicit return value, which is used internally by the system. This function can have either a parameter or multiple parameters.
(3) constructor can be overloaded, that is, you can define a function with different numbers of parameters.
(4) The constructor cannot be called directly in the program. when an object is created, the system automatically calls the constructor.
The features of destructor are as follows::
(1) The Destructor is a special member function with the same name and "~" is added before it. Character, used to distinguish it from the constructor. The Destructor does not specify the data type and has no parameters.
(2) Only one destructor can be defined in a class, and the Destructor cannot be overloaded.
(3) destructor can be called, such as B .~ Base ();) can also be called by the system. In the following two cases, the Destructor is automatically called. First, if an object is defined in a function, the destructor of the object is automatically called when the function ends; second, when an object is passively created using the new operator and released using the delete operator, delete will automatically call the destructor.

 

The delete statement can only release the memory occupied by the object itself (delete is used to release the memory occupied by the object itself,Solve this problem)
If the object opens a file, data connection, or other resources are not released during its lifetime, before the object is destroyed, the Destructor provides an opportunity for programmers to close files and data connections and release other resources. If these resources are not released, a large amount of memory will be consumed as the program runs.

 

 

 

Virtual functions:

After a member function in the base class is declared as a virtual function, the virtual function can be redefined in one or more Derived classes.

When being redefined in a derived class, its function prototype, including the return type, function name, number of parameters, parameter type, and parameter sequence, must be exactly the same as the prototype in the base class.

 

 

 

 

Delete P is a two-step process: Call the Destructor and release the memory.

The Destructor provides a mechanism opposite to the constructor that allows you to reclaim the space occupied by an object before it is destroyed) let the object release its own resources (this sentence is very subtle, that is, how the memory occupied by the object is released in front of it, the structure then solves the resources used by the object, such as char * P = new Char. This also solves the reason why the common constructor does not use new, and the Destructor does not use Delete)

 

Enumeration type usage problems:

The enumeration type in C ++ inherits from the C language. Just like many other features inherited from the C language, the C ++ enumeration also has disadvantages. The most significant one is the scope problem-a constant defined in the enumeration type, it belongs to the scope of the defined enumeration, and does not belong to this enumeration type. For example:

Enum fileaccess {
Read = 0x1,
Write = 0x2,
};

Fileaccess access =: read; // correct
Fileaccess access = fileaccess: read; // Error

This feature of C ++ enumeration is unacceptable for those who are used to object-oriented and scope concepts.

 

Function pointer Array:

# Include <stdio. h>
Int fun (int)
{
Return A + 1;
}
Int main ()
{
INT (* P [10]) (INT );
Int I;
P [0] = fun;
I = (P [0]) (10 );
Printf ("I = % d/N", I );
Return 0;
}
P [10] is a pointer array, and the value in the pointer array is of the function pointer type. point P [0] to the fun () function. then you can call the fun () function.

 

Class to access non-static members:

Static base: FUNA (base * P)

{

P-> m_num =...; // probably this form

}

 

 

# UNDEF is defined later to cancel the previous definitionMacro definition(Cancel macro definition)

Trace macro

Trace macro is very useful for program debugging in VC and has functions similar to printf. This macro only appears in the debug version of the program, when release is enabled, the macro disappears completely, helping you reduce the amount of code when debugging is also performed.

The format is as follows:

Trace ("ddddddddddd ");
Trace ("Wewe % d", 333 );
There are also trace0, trace1, trace2... Corresponding to 0, 1, 2, respectively .. Parameters

 

COM interface:

Is a C ++ base class, which defines a set of pure virtual functions (only pure virtual functions). These functions fully control the behavior of Their Derived classes.

1) An interface is a set of rules that specifies a group of rules that must be owned by the class or interface that implements this interface. It reflects the natural world, "if you are ...... You must be able ......" Concept.

2) an interface is an abstract representation of similar things in a certain granularity view. Note that I have emphasized a certain granularity view, because the concept of "similar things" is relative, it varies with the granularity view.

Interface-Oriented Programming: In System Analysis and architecture, layers and dependencies are distinguished. Each layer does not directly provide services to the upper layer (that is, it is not directly instantiated in the upper layer ), instead, define a group of interfaces to expose only the interface functions of the upper layer. The upper layer only depends on the lower layer and does not depend on the specific class.

 

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.