Essential C ++

Source: Internet
Author: User

1. Local static object-- Heap object?

2. Default Parameters-- 1. It can only be defined once (header file or CPP file ). 2. The default value of resolution (RESOLVE) starts from the rightmost.
It means that void fun (int A = 0, int B, int c = 2) is incorrect.

3. Overload vs Template
Overload -- Multi-copy implementation
Template-subject unchanged, data type changed

Template Definition
Template <typename eletype> fun (const vector <eletype> & VEC ){
For (INT I = 0; I <Vec. Size (); ++ I)
Eletype T = VEC [I];
...
}

4. function pointer
Definition of common functions
E. g
Const vector <int> * fibon_seq (INT size)

Function pointer Definition
E. g
Const vector <int> * (* seq_ptr) (INT)

Function pointer example
Bool seq_elem (INT POs, Int & ELEM, const vector <int> * (* seq_ptr) (INT )){
Const vector <int> * pseq = seq_ptr (POS); // call the function pointer in the same way as normal functions.
If (! Pseq) // check
....
}

The initial value of the function pointer can be 0.
Const vector <int> * (* seq_ptr) (INT) = 0;
It can also be the address of a function.
Const vector <int> * (* seq_ptr) (INT) = maid;

5. inline
Inline must be defined in the header file

6. Include
Include <*. h>-<> indicates the header file in the standard library.
Include "*. h"-"" indicates the custom file

7. Array
The array is passed in as a function parameter, and the address of the first element is passed in.

8. Vector vs list vs deque
Vector-continuous memory stores sequence data. (Array) High random read Efficiency
List-two-way linked list stores sequence data. High insert/delete Efficiency
Deque-similar to vector, it is highly efficient to insert and delete data at the beginning and end to implement queue.
Directly use [] to access the serialization container

9. Reference, pointer
Reference: always points to the object pointed to when the first value is assigned.
When the pointer is used as a function parameter to pass an object, the effect is the same:
1. You can directly modify the object of a person.
2. Do not copy the passed object. The reference copies the object address, and the pointer copies the pointer itself.

References are superior to pointers:
Reference can avoid NULL pointer (* PT) errors.
When the pointer is retrieved, first judge whether the pointer is changed to 0. The reference does not need to be determined in this way.
If (PT! = 0 & * PT = ...)

Avoid mistakes. (when an external call is used, the declared object rather than the pointer will be selected in inertia to reduce the chance of Memory leakage errors)

9.1 function parameters are referenced definitions.
Public void fun (A & ){
A B (3 );
A = B;
// Or
// A * c = new A (3 );
// A = * C;
// Delete;
 
}
Use
A A (5 );
Fun (a); // after execution a = 3

9.2 function parameters are pointers
Public void fun (A * ){
A B (3 );
* A = B; // Dereference)
}

Use
A * pA = new A (5 );
Fun (PA); // after execution, the PA content is 3.
//... Use Pa
Delete Pa;

Both Java function parameter passing and C ++ default function parameter passing are passed values. C ++ can also transmit references.
Any operation on the referenced object is equivalent to an operation on the passed object. Including assigning a new object.
Function calls in Java are not allowed. For example
Void javafun (){
A = new ();
}
In this way, after the Java function is called, a still maintains the value when it is called.

But in C ++
Void cplusfun (A & ){
A B;
A = B;
}
The content of a has changed after the function is called.

10. Stack object vs stack object
Stack objects (such as objects defined in functions) are temporarily placed in the stack, function execution is completed, and content in the stack is discarded.
Heap objects (such as objects with dynamically allocated memory) are managed by the programmer.

11. function object
A class object that overload function call :() is used to define operators. Its function is similar to the function pointer, but it is more efficient than the function pointer,
Make the call operator inline. And can be kept (class objects are saved)

Bind2nd and bind1nd are also function objects, which convert binary function objects into one dollar. Another parameter is specified by Bing * nd constructor parameters.

12. pointer, Pointer Reference
To change the pointer content, you only need to pass the pointer.
To change the pointer itself, you must pass the pointer or pointer reference.

Chapter 4 object-based programming style
1. pre-declaration
Forward Declaration: defines class pointers or uses this class as the data type. Visible before Compilation
The function is similar to include. If nested include is used, the other class uses the pre-declaration.
E. g Class stack;
 
2. inline
Specify the keyword inline directly when defining the header file or when defining the function in the CPP file.

3. Constructors
Default constructors may provide default values for each parameter.
E. g
Class trigangular {
Triangular (INT Len = 1, int BP = 1) // It is also the default constructor.
}
 
Trigangular T = 8; // only one constructor is called, not the value assignment operator (assignment operator)
 
4. Member initial value table (member initialization List)
The member initialization list is followed by the colon in the constructor parameter table and is separated by commas (,). It is used to pass parameters to member variables.
E. g Class trigangular (const trigangular & RHs): _ length (RHS. _ length), _ next (RHS. _ next)
{
}
What is the principle of membership initial value table over value assignment (when member object is used?

5. copy constructor and copy assignment operator
Default member initialization behavior (memberwise initialization)
E. g trigangular T1 (8 );
Trigangular t2 = T1;
Each member variable of T1 is assigned to each member variable of T2 by default.
If the class contains a member variable of the pointer, the default initialization will point the pointers of both objects to the same address space.
The pointer to the memory is released, while the T2 pointer still points to the memory. Error!

Copy constructor should be provided
Matrix: matrix (const Matrix & RHs): _ row (RHS. _ row), _ COL (RHS. _ ROL)
{
Int num = _ row * _ Col;
_ Pmat = new double (Num );

For (INT I = 0; I <num; I ++)
{
_ Pmat [I] = RHS. _ pmat [I];
}
}
 
Copy assignment operator
Matrix & matrix: Operator = (const Matrix & RHs)
{
If (this! = & RHs)
{
_ ROW = RHS. _ row; _ Col = RHS. _ Col;
Int num = _ row * _ Col;

Delete [] _ pmat;
_ Pmat = new double (Num );

For (INT I = 0; I <num; I ++)
{
_ Pmat [I] = RHS. _ pmat [I];
}
}
Return * this;
}
The implementation of copy assignment operator is similar to that of copy constructor. Note the following:
1. Determine whether the assigned object is the assigned object itself.
2. Release the original dynamically allocated memory before starting replication.
 
6. Const
Compiler guarantee
1. The implementation of const member function cannot change the class object.
2. the const object cannot call its own non-const member function.
 
Member function can be reloaded based on whether the const is enabled or not.
E. g
Class {
Const bigclass Val () const {return _ Val;} // 1
// The first const indicates that the bigclass object of const is returned, and the second const indicates that this method cannot change the class object.
Bigclass Val () {return _ Val;} // 2
}
 
Void example (const A & A, a B)
{
A. Val (); // invoke 1
B. Val (); // invoke 2
}
 
7. mutable (variable)
If you want to declare the method as const, the class object will be changed in the method (In a sense, this change will not change
The Class Object State does not damage the constant of the object. You can declare this variable as mutable.
 
Class {
Public:
Void next () const {_ next = _ beg_pos-1;} // OK
PRIVATE:
Mutable int _ next;
Int _ beg_pos;
}
 
8. static member functions
Statement:
Class {
Static bool is_elem (int elem );
}

Definition
Bool a: is_elem (int elem) {...} // no need to add static

Use
Boolean is_elem = A: is_elem (ival );

9. Operator Overloading
9.1 Rules:
1. do not introduce new operators
2. The four operators are not reloaded.
.
.*
::
?
3. The operands cannot be changed.
4. The priority cannot be changed.
5. The parameter must have a class type ??
 
9.2 Definition
Member function:
Int t_iterator: Operator * () const
{
...
}

Non-member function:
Int operator * (const t_iterator & RHs ){....}

The non-member function operator has one more parameter (this of the member operator) than the member function operator, representing the left operand.

9.3 front and rear
Pre-defined (++ T)
T_iterator & t_iterator: Operator ++ (){
++ _ Index;
Return * this;
}

Post definition (T ++)
T_iterator: Operator ++ (INT ){
T_iterator TEM = * this;
++ _ Index;
Return TEM;
}
Because the parameters must be different in the overload rule, C ++ requires that the rear version have an int parameter to be distinguished from the front.
The compiler automatically generates an int reference (0) for the post function ).

9.4 function object overload ()
Concept: function object is a class that provides function call implementation.

Description and definition
Class less {
Less (INT Val): _ Val (VAL ){};
Boolean operator () (INT value) const {return value <_ Val ;};
}

Use
Int count_less_than (const vector <int> & VEC, int comp ){
Less LT (COMP );
Int COUNT = 0;
 
For (INT I = 0; I <Vec. Size (); I ++ ){
If (LT (VEC [I])
Count ++;
}
Return count;
}

10. pointer to a class member function
10.1 Definition
E. g void (classa: * PM) (INT) = 0;
Or
Typedef void (classa: * ptrtype) (INT );
Ptrtype PM = 0;

10.2 assignment
Ptrtype PM = & classa: functiona;

10.3 use
Classa;
Ptrtype PM = & classa: functiona;
(A. * PM) (POS); // equivalent to a. functiona (POS );

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.