C ++ Part 2

Source: Internet
Author: User

These were all written at school in, and they were placed on the csdn blog. Now we can sort them out.

1. Compatibility of Values

Objects of a public derived class can be used as objects of the base class. Otherwise, they are not allowed. The specific performance is as follows:
The object of the derived class can be assigned to the Base Class Object.
The object of the derived class can initialize the reference of the base class.
The pointer to the base class can also point to the derived class.

 

2. Static and Dynamic Association

Static Association and Dynamic Association
Joint Editing:
The program itself is associated with each other to determine the relationship between the operation calls in the program and the code that executes the operation.
Static Association (static bundle)
In the compilation stage, the object name or class name is used to define the function to be called.
Dynamic Association
The function to be called is determined only when the program is running.

Virtual functions:
Virtual functions are the basis for dynamic Association.
1. Non-static member functions.
2. In the class declaration, write virtual before the function prototype.
3. Virtual is only used to describe the prototype in the class declaration and cannot be used in function implementation.
4. It has inheritance, and the virtual function is declared in the base class. In the derived class, the original function is automatically a virtual function regardless of the description.
5. essence: it is not a heavy load declaration, but a coverage. (Because it does not belong to the function signature part)
6. Call method: The base class pointer or reference determines which function to call based on the class of the object to which the Pointer Points during execution.

Virtual destructor:
To ensure that the Destructor can be correctly called, The Destructor are generally declared as virtual functions (unless such functions are not inherited) And we call them virtual destructor.
For example:
Class base
{
Public:
Virtual ~ Base () {cout <"base destructor \ n ";}
};

Class derived
{
Public:
Virtual ~ Derived () {cout <"derived destructor \ n ";}
};

Int main ()
{
Base * B = new derived ();
Delete B;
}

Output:
Derived destructor
Base destructor

I have made such a note before, but I am not very clear about it now. Note:
There is a problem when using polymorphism to process objects in dynamically allocated class hierarchies.
If the delete operator is used to point to the base class pointer of a derived class object, and the program explicitly calls this operator to delete each object,
No matter what type the base class Pointer Points to, and ignore the different destructor names of each class, the system will call the base class destructor for these objects.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chusky/archive/2006/02/21/605099.aspx

3. Conversion between base class pointers and derived class pointers

(1) The base class pointer cannot be directly assigned to the class pointer.
(2) If the display type conversion is used, it is the programmer's responsibility to correctly use pointers.
(3) because the base class Pointer Points to the base class object, after forced conversion, the derived class pointer can correctly access the data in the base class object,
However, the data of the derived class object does not actually exist. It is dangerous to forcibly access the data.

Base Class pointer, derived class pointer, base class object, mixed matching of derived class object:
(1) directly reference the base class object with the base class pointer
(2) directly reference the object of the derived class using the pointer of the derived class
(3) use a base class pointer to reference a derived class object. Because the derived class object is also a base class object, this reference is safe,
However, you can only reference base class members. The compiler reports a syntax error if you try to reference only the Members in the derived class through the base class pointer.
(The answer to this question is virtual functions and polymorphism)
(4) use the pointer of the derived class to reference the objects of the base class. This reference method may cause syntax errors. A pointer to a derived class must be forcibly converted to a base class pointer.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chusky/archive/2006/02/21/605096.aspx

4, volatile (Reading Notes)

The grammar of volatile is the same with that of const, but volatile means "this data may
Be changed besides the compiler's eyeshot ". For some reason, may be multitask, multithread,
Interrupt, this data are changed by the enviroment, So we declare it volatile to tell
Compiler not to do any Optimization on it.
If the complier says, "I have read the data into a register, and havene' t attained to it
Since that ", in common, it doesn't need to read it any more. But if it was specified by vo-
Latile, the compiler must read it again.

// Volatile. cpp
// The volatile keyword

Class comm
{
Const volatile unsigned char byte;
Volatile unsigned char flag;
Enum {bufsize = 100 };
Unsigned char Buf [bufsize];
Int index;
Public:
Comm ();
Void ISR () volatile;
Char read (INT index) const;
};

COMM: comm (): Index (0), byte (0), flag (0 ){}

// Only a demo: won't actually work
// As an interrupt service routine:
Void COMM: ISR () Volatile
{
Flag = 0;
Buf [index ++] = byte;
// Wrap to beginning of buffer:
If (index> = buffsize) Index = 0;
}

Char COMM: Read (INT index) const
{
If (index <0 | index> = bufsize)
Return 0;
Return Buf [Index];
}

Int main ()
{
Volatile COMM port;
Port. ISR (); // OK
Port. Read (0); // error, read () not volatile
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chusky/archive/2006/02/22/606326.aspx

5. Summary of abnormal knowledge points in C ++

Catch (...) catch all exceptions
Sometimes throw is used in catch () {}, which means no processing is performed on the caught exception, but throw the exception and leave the null statement outside the Catch Block, executing this throw will call terminate.
It is a logical error to place an exception processor with the void * parameter type before an exception processor with other pointer types, because it will capture all exceptions and the latter will not work at all.
When the try block is executed normally without throwing any exception, the first statement after the catch processor after the try block is passed in instead of the first statement after the try block.
The Catch Block can also throw an exception, but the exception is handled by the exception processor in the outer try block.

If an exception is specified, a column of exceptions can be thrown by the specified function:
Int g (float h) Throw (A, B, C)
{...}

This can limit the exception type thrown by the slave function. In fact, this can also throw other exception types and call unexpected.
If throw () is null, it means no exception is thrown, but an exception can still be thrown. However, calling unexcepted can capture the pointer or reference of an exception object in each cell of the derived class using the exception mechanism, but the more concise method is to capture the pointer and reference of the exception object. This avoids errors. In addition, we can identify exceptions by setting object attributes.

Example of throwing an exception again:
# Include <iostream>
# Include <exception>

Using namespace STD;

Void throwexception () Throw (exception)
{
Try
{
Cout <"function throwexception \ n ";
Throw exception ();
}

Catch (exception E)
{
Cout <"exception handled in function throwexception \ n ";
Throw;
}
}

Int main ()
{
Try
{
Throwexception ();
Cout <"This shoule not print \ n ";
}
Catch (exception E)
{
Cout <"exception handled in main \ n ";
}

Cout <"Program Control continues after catch in main" <Endl;
Return 0;
}
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chusky/archive/2006/02/22/606334.aspx

6. inline (Reading Notes)
Inline is a new characteristic in the C ++ language. the compiler will check the type of it, if there is not error, the code of the inline function will be put where it is invoked. while the # define macro is processed by the Preprocessor, which wocould not check the type.
There two restriction:
1, the inline fuction is too complex, e.g. Has loop setences.
2, the function address is needed explicitly or inexplicitly.
In this case, the complier will drop the inline, and treat as the common function.
The inline function is often defined in the head file, and is treated as static function in those files which inlude this head file, make it local to avoid re-defined errors.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chusky/archive/2006/02/21/605091.aspx

7. Do not return a pointer to a local variable because the local variable is stored on the stack and is released once the function call ends. Except for static local variables, the lifetime is global (created when the statement is executed for the first time, which is different from the global (static) variable), and the scope is local

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.