C ++ notes

Source: Internet
Author: User
Tags bitset

----------------------------------------------

// OK

Int ivals (1024 );

Int ival (INT ());

 

// Each built-in data type supports a special constructor syntax to initialize the object to 0

Int ival = int ();

-------------------------------------------------

Any attempt to direct a non-const object pointer to a constant object will cause compilation errors,

The address of a const object can only be assigned a pointer to a const object, but a pointer to a const object can be assigned a non-const object address.

Const double minwage = 9.60;
// Error!
Double * PTR = & minwage;

--------------------------------------------------------

Constant pointer: a pointer to a constant. The content of the address pointed to by the pointer cannot be modified.

For example, const int * P = & A; (P points to const INT)

Pointer constant: the constant of the pointer. It is a pointer that cannot change the address, but can modify the content it points.

For example: int * const P = & A; (const P points to int)

-------------------------------------------------------------

Reference Type: Reference is sometimes called an alias. It can be used as another name of an object.

Int ivals = 1024;
Int & refval = ival;

------------------------------------------------------

Enumeration type usage

Enum open_modes {input = 2, output, append };
Open_modes om = output;
Cout <om <Endl;

// Point2d = 2, point2w = 3, point3d = 3, point3w = 4
Enum points {point2d = 2, point2w, point3d = 3, point3w };

Void mumble (){
Points pt3d = point3d; // OK: pt3d = 3
// Error pt2w is initialized as an int integer
Points pt2w = 3;
// Error: polygon is not an enumeration member of points.
Pt2w = polygon;
// OK: Both pt2w and pt3d are of the points Enumeration type.
Pt2w = pt3d;
}
However, if necessary, the enumeration type is automatically upgraded to the arithmetic type. For example
Const int maid = 1024;
// OK: The pt2w is upgraded to the int type.
Int chunk_size = array_size * pt2w;

----------------------------------------------------

Iterator usage

Vector <string> text;

For (vector <string >:: iterator it = text. Begin ();
It! = Text. End (); ++ it)
Cout <* It <'';

// OK

Int Ia [7] = {0, 1, 1, 2, 3, 5, 8 };
Vector <int> ivec1 (IA, Ia + 5 );

------------------------------------------------------

The following is a problem that almost everyone will get wrong at the beginning. The error is that typedef is used as a macro extension.
The following typedef
Typedef char * cstring;
In the following declaration, what is the CSTR type?
Extern const cstring CSTR;
The first answer is almost always
Const char * CSTR
That is, the pointer pointing to the const character, but this is incorrect.
Therefore, this definition declares that CSTR is a const pointer to a character. For details, see section 3.5 about the const pointer type.
.
Char * const CSTR;

------------------------------------------------------------

The main purpose of the volatile modifier is to prompt the compiler that the value of this object may be
Therefore, the compiler cannot arbitrarily referenceCodeOptimization

------------------------------------------------------------

PairClasses are also part of the standard library, so that we can put the same type or different types
To use the pair class, we must include the following header file.
# Include <utility>
For example
Pair <string, string> author ("James", "Joyce ");
Creates a pair object author, which contains two strings initialized as James and Joyce respectively.
We can use the member access symbol member access notation to access the names of individual elements in pair.
For example
String firstbook;
If (author. First = "James "&&
Author. Second = "Joyce ")
Firstbook = "Stephen Hero ";
If we want to define a large number of objects of the same pair type, the most convenient way is to use typedef as follows:
Display
Typedef pair <string, string> authors;
Authors Proust ("Marcel", "Proust ");
Authors Joyce ("James", "Joyce ");
Authors Musil ("Robert", "Musil ");

--------------------------------------------------------------

Forward Declaration

You can declare a class without defining it.
Class Screen; // Declaration of the screen class
This statement is sometimes called Forward Declaration.ProgramIntroduced the class type of screen. before the definition, the class screen is an incomplete type (incompete type), that is, it is known that the screen is a type, but does not know which members are included.
Incomplete types can only be used in a limited manner, and objects of this type cannot be defined. Incomplete types can only be used to define pointers and references pointing to this type, or to declare (rather than define) function that uses this type as the parameter type or return type.

// Forward Declaration (Forward Declaration)
Class entryslot;
Extern entryslot * look_up (string );
Typedef pair <string, entryslot *> symbolentry;
Symbolentry current_entry ("author", look_up ("author "));

//...
If (entryslot * It = look_up ("Editor "))
{
Current_entry.first = "Editor ";
Current_entry.second = it;
}

--------------------------------------------------------------

Operator overload

The overloaded operators use the following general form:
Return_type operator OP (parameter_list );
Here operator is the keyword op, which is a predefined operator such as + = []

Char & operator [] (INT );
An overloaded instance that declares a subscript operator. It carries an int type parameter and returns a reference pointing to Char.

For example
// Put it in the program text file: String. c
// Contains the definition of the string class
# Include "string. H"
// Declaration containing the strcmp () function
// Cstring is the header file of the Standard C library
# Include <cstring>
Bool // return type
String: // indicates that this is a member of the string class.
Operator ==/// function name: equal to operator
(Const string & RHs) // list of parameters
{
If (_ size! = RHS. _ SIZE)
Return false;
Return strcmp (_ string, RHS. _ string )? False: true;
}

Inline string &
String: Operator = (const string & RHs)
{
If (this! = & RHs)

 

{
Delete [] _ string;
_ Size = RHS. _ size;
If (! RHS. _ string)
_ String = 0;
Else {
_ String = new char [_ SIZE + 1];
Strcpy (_ string, RHS. _ string );
}
}
Return * this;
}

====================

Bitset <32> bitvec;

Define 32-bit bitset

----------------------------------

Explicit Conversion

Explicit conversions are also called forced conversions. cast includes the following mandatory conversion operators.
Static_cast dynamic_cast const_cast and reinterpret_cast although sometimes it is necessary to force type conversion
In other words, they are also the source of program errors. By using them, programmers disable the C ++ language type check settings.
Before learning how to forcibly convert a value from one type to another, let's first look at when
To do so
Any non-const data type pointer can be assigned to the void * type pointer, the void * type pointer is used when the exact type of the object is unknown or the object type changes in a specific environment, sometimes the void * type pointer is called a generic pointer because it can pointer to any data type

Int ival;
Int * Pi = 0;
Char * Pc = 0;
Void * PV;
Pv = PI; // OK: implicit conversion
Pv = pc; // OK: implicit conversion
Const int * PCI = & ival;
Pv = PCI; // error: PV is not a const void *.
Const void * PCV = PCI; // OK

When a void * pointer is assigned to any explicit type, C ++ requires explicit forced conversion.

Bytes ----------------------------------------------------------------------------------------

Function pointer

# Include <iostream>
Int min (int *, INT );
INT (* PF) (int *, INT) = min;
Const int iasize = 5;
Int Ia [iasize] = {7, 4, 9, 2, 5 };
Int main (){
Cout <"direct call: min :"

<Min (IA, iasize) <Endl;
Cout <"indirect call: min :"
<PF (IA, iasize) <Endl;
Return 0;
}
Int min (int * IA, int sz ){
Int minval = Ia [0];
For (int ix = 1; ix <SZ; ++ IX)
If (minval> Ia [ix])
Minval = Ia [ix];
Return minval;
}
Call
PF (IA, iasize );
You can also use an explicit pointer symbol to write
(* PF) (IA, iasize );

 

We can declare an array of function pointers, for example
INT (* testcases [10]) ();
Declare testcases as an array with 10 elements. Each element is a function pointer to the function.
No parameter of this function. The return type is int.
Declarations such as the array testcases are very difficult to read, because it is difficult to analyze the function type and part of the Declaration.
In this case, the typedef name can make the declaration easier to read, for example
// Typedefs makes the Declaration easier to read
Typedef int (* PFV) (); // Type Def that defines the function type pointer
PFV testcases [10];
This declaration of testcases is equivalent to the previous one.

[] The priority is greater than *, and * is the combination from right to left.

Pointer ArrayAs the name suggests, the first thing isAn arrayThen the elements in the array are pointers.

Description form: type * pointer_array [constant1] [constant2]... [constantn];

Array pointer: Pointer to an array. Description: type (* pointer_array) [constant1] [constant2]... [constantn]; int (* AP) [2 ];OnePoint to an array containing an integer array containing two elementsPointer

-------------------------------------------------------

Function Template

Template <class type>
Type min (type A, type B ){
Return a <B? A: B;
}

 

Template <typename type, int size>
Type min (type (& r_array) [size])
{
Type min_val = r_array [0];
For (INT I = 1; I <size; ++ I)
If (r_array [I] <min_val)
Min_val = r_array [I];
Return min_val;
}

Int Ia [] = {10, 7, 14, 3, 25 };
Int I = min (IA );

----------------------------------------------------

Virtual void finalize_state () = 0; // pure virtual function, which must be implemented in the subclass

Virtual void finalize_state (); // virtual function

---------------------------------------------

String STR () const;

After the function declaration, add const to indicate that the function cannot change the value of the member variable in the function implementation.

 

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.