Deep Exploration C ++ object model Reading Notes (6)

Source: Internet
Author: User
Deep Exploration C ++ object model Reading Notes (6 ).

* ** Object Construction and deconstruct ***

In general, we will try to place the object near the program segment where it is used, which can save unnecessary operations on generating and destroying the object.

* ** Global Object ***

Static initialization policies for global objects include the following steps:

(1) generate a _ STI _...... () Function, containing necessary constructor call operations or Inline expansions;

(2) generate a _ STD _...... () Function, containing the necessary destructor call operations or Inline expansions;

(3) Add a _ main () function (used to call all _ STI () functions in the executable file) and a _ exit () function at the beginning and end of the Main () function () function (used to call all _ STD () functions in an executable file ).

We recommend that you do not use global objects that require static initialization.

* ** Local static object ***

Suppose we have the following program snippets:

 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 executed only once, although the above functions may be called multiple times;

(B) The destructor of mat_identity must be executed only once, although the above functions may be called multiple times.

The actual practice of the compiler is as follows: Construct mat_identity when identity () is called for the first time, and deconstruct it in the static memory release function associated with the corresponding file. (The address of a local static object in downstream component will be converted to the Data Segment used to place the global object in the program)

* ** Object array ***

If you want to retrieve the constructor address in the program, this is not acceptable. However, activating constructor through a pointer will not be able to access default argument values. How can we support the following statements:

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

When a programmer writes:

 complex c_array[10];

The compiler eventually needs to call:

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

To solve this problem, the compiler can generate an internal constructor without parameters. In its function, the constructor provided by the programmer is called and the default parameter value is explicitly specified:

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

* ** New and delete operators ***

Use constructor to configure a class object:

     Point3d *origin = new Point3d;

Converted:

Point3d * origin;
If (origin = _ new (sizeof (point3d ))){
Try {
Origin = point3d: point3d (origin );
}
Catch (...){
_ Delete (origin); // release memory configured for new
Throw; // upload the original exception
}
}

If we configure an array with 10 point3d objects in it, we expect the point and point3d constructor to be called 10 times each time, acting on one element in the array each time:

// This is not a good idea.
Point * PTR = new point3d [10];

If we perform the following release operations:

// Only point ::~ Point called
Delete [] PTR;

Because the vec_delete () triggered by vec_delete () passes through each array element through iteration, the size of the Point class object passed in this example is not the size of the point3d class object, the entire running process will fail.

The solution lies in the program level, rather than the non-verbal level:

 for(int ix = 0; ix < 10; ix++)
{
Point3d *p = &((Point3d*)ptr)[ix];
delete p;
}

Of course, it is best to avoid pointing a base class pointer to an array composed of a derived class objects.

Series of articles:

Deep Exploration C ++ object model Reading Notes (1)

Deep Exploration C ++ object model Reading Notes (2)

Deep Exploration C ++ object model Reading Notes (3)

Deep Exploration C ++ object model Reading Notes (4)

Deep Exploration C ++ object model Reading Notes (5)

Deep Exploration C ++ object model Reading Notes (7)

Last reading note of Deep Exploration C ++ Object Model

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.