C ++ knowledge review and related knowledge Review

Source: Internet
Author: User

C ++ knowledge review and related knowledge Review

1. typedef

Typedef is used to define synonyms of the same type. For example:

1 typedef unsingned int size_t;2 typedef int ptrdiff_t;3 typedef T * iterator;

Note that typedef should not be viewed as a text extension, as shown in the following example:

1 typedef string * pstring;2 const pstrng cstr;

What type should cstr represent in the second row? If it is used as a text extension, cstr indicates const string *, that is, cstr is a pointer pointing to a const string object. But in fact, const modifies pstring type, while pstring is a string type pointer, cstr should represent a const pointer to a string type object.

 

2. sizeof Operator

The sizeof operator is used to return the length of an object or type name. The type of the returned value is size_t, And the length is measured in bytes.

1 // three methods of the sizeof operator 2 sizeof (type name); 3 sizeof (expr); 4 sizeof expr; 5 // eg. 6 string ss, * sptr; 7 int I; 8 sizeof (ss); 9 sizeof (string); 10 sizeof * sptr; 11 sizeof (I); 12 sizeof (int );

You can use the sizeof operator to calculate the number of elements in an array, as shown below:

1 int a[10];2 int sz = sizeof(a)/sizeof(int);//sz == 10

 

3. display type conversion (forced type conversion)

The forced type conversion symbol for naming is generally in the following format:

1 cast-name<type>(expression);

A. dynamic_cast

The dynamic_cast operator converts a reference or pointer of a base class object to a reference or pointer of another type in the same inheritance level.

The difference between dynamic_cast and other forced conversions is that dynamic_cast involves runtime type checks. If the object bound to the reference or pointer is not of the target type, dynamic_cast fails. If the conversion to dynamic_cast of the pointer type fails, the result of dynamic_cast is 0. If the conversion to dynamic_cast of the reference type fails, a bad_cast exception is thrown. Let's look at an example:

 1 class Base{ 2 ... 3 }; 4  5 class Derived:public Base{ 6 ... 7 }; 8  9 //Base *baseptr = new Base;10 Base *baseptr = new Derived;11 if (Derived *derivedPtr = dynamic_cast<Derived *>(baseptr)){12 ...13 }else{14 ...15 }

If baseptr actually points to the Derived object, the conversion is successful. If baseptr points to the Base object, the conversion fails.

B. const_cast

Const_cast can be used to convert the const attribute in the expression. Example:

1 void process(string *sptr);2 const string *csptr;3 process(const_cast<string *>(cspttr));

The function process accepts the string * parameter. If it is not possible to pass it a const string *, you can use const_cast to convert const string * to string *.

B. static_cast

Any type of quasi-change that is implicitly executed by the compiler can be explicitly completed by static_cast.

1 double dval = 3.14;2 char cval = static_cast<char>(dval);

You can also use static_cast to retrieve the value stored in the void * pointer.

1 double dval = 3.14;2 void * ptr = &dval;3 double *dp = static_cast<double *>(ptr);

 

4. Three new types in c ++

A. new Expression

The new expression dynamically creates an object and returns a pointer to the newly created object, which is created in the free storage area. The memory of the object created using the new expression must be released using the delete expression. If you forget to release the object, memory leakage will occur. When the new expression cannot obtain the required memory space, the system throws a bad_alloc exception.

1 int *pi = new int(10);2 int *pj = new int;3 string *ps = new string("1024");4 string *psj = new string;

When the new expression is used, three steps are taken. First, the expression calls the operator new () operator function to allocate enough uninitialized memory to save an object of the specified type. Then, run a constructor of this type, constructs an object on the uninitialized memory with the specified initialization type. Finally, a pointer to the newly allocated and constructed object is returned.

B. operator new and operator delete

Operator new is used to obtain uninitialized memory, which is similar to the allocate member function in the allocator class; operator delete is used to release the memory, which is similar to the deallocate () member function in the allocator class. Operator delete does not run the destructor. Therefore, before calling the operator delete operation, you must clear the object by calling the destructor. The prototype of operator new and operator delete is as follows:

 1 void *operator new(size_t); 2 void *operator new[](size_t); 3 void *operator delete(void *); 4 void *operator delete[](void *); 5  6 //eg. 7 string *ps = static_cast<string *>(operator new(sizeof(string))); 8 int sz = 10; 9 int *pi = static_cast<int *>(operator new(sizeof(int)*sz));10 operator delete[](pi);

C. Positioning new (placement new)

Locate the new expression to initialize an object in the allocated original memory. It differs from operator new in that it does not allocate memory objects. On the contrary, it accepts pointers to allocated but unconstructed memory and initializes an object in the memory. This is similar to the construct member function in the allocator class. The new expression is as follows:

1 new (place_address) type;2 new (place_address) type(initializer_list);3 4 //eg.5 string *ps = static_cast<string *>(operator new(sizeof(string)));6 new (ps) string("hello world");

 

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.