"Deep Exploration C + + object Model" Reading notes (6)

Source: Internet
Author: User
Tags constructor object model static class

Construction and deconstruction of objects * * *

In general, we would place the object as far as possible near the program section where it was used, which would save unnecessary objects from being manipulated and destroyed.

Global Objects * * *

The static initialization policies for global objects include the following steps:

(1) Create a _sti_ for each object that needs to be statically initialized ... () function that contains the necessary constructor call operations or inline expansions;

(2) Create a _std_ for each object that requires a static memory release operation ... () function that contains the necessary destructor call operations or inline expansions;

(3) Add a _main () function (to invoke all the _sti () functions in the executable) and a _exit () function (to invoke all the _STD () functions in the executable file) at the end of the main () function.

It is recommended that you do not use global objects that require static initialization at all.

Local Static Object * * *

Suppose we have the following program fragment:

const Matrix& identity() {
static Matrix mat_identity;
// ...
return mat_identity;
}

The local static class object here guarantees the following semantics:

(a) The constructor of Mat_identity must be performed only once, although the function may be invoked multiple times;

(b) The destructor of mat_identity must be performed only once, although the functions mentioned above may be invoked multiple times.

The compiler's practice is as follows: The mat_identity is constructed when the identity () is first invoked, and is refactored in the static memory release function associated with the corresponding file. (The address of a local static object will be converted to the data segment within the program to place the global object in downstream component)

Object Array * * *

This is not possible if you want to remove a constructor address from the program. However, by activating constructor via a pointer, default argument values will not be accessible. Then, how to support the following statement:

complex::complex(double=0.0, double=0.0);

When the programmer writes:

complex c_array[10];

, the compiler eventually needs to invoke:

vec_new(&c_array,sizeof(complex),10,&complex::complex,0);

To solve this problem, the compiler can generate an internal constructor with no parameters, invoke the programmer-supplied constructor within its function, and specify the default parameter value explicitly in the past:

complex::complex()
{
complex(0.0, 0.0);
}

Related Article

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.