How to get the number of constructors and Copy Constructors

zhaozj2021-02-11  202

How to get the number of constructors and Copy Constructionors

Darkspy @ 2002/6/24 @ http://conicos.126.com

We know that the compiler is constructed when constructing an object, if the programmer itself does not define the constructor, in some situation, the compiler will synthesize one if our object has Memberwise, The compiler will synthesize an Operator = and COPY Constructor, the compiler will perform the COPY operation of Data Member, as some programmers who don't know C internal operations, how do you know how Copy Constructors do?

Don't try to use simple int to accumulate Constructor and COPY Constructor, if the int change is in the class, then constructs once when the constructor is time, the destructor is changing, you can't get the right number of execution, maybe you Will say: I can define the intimate variable. Try it, we absolutely unable to get an object call several constructors and Copy Constructors, and the numbers have deviations.

so what should I do now?

We can define a simple class, define proprietary operations, such as operators, and simple constructor,

#include struct c {int count; c (): count (0) {} int operator (int) {return count = 1;} // overload operator};

Ok, after defining this foundation COUNT class, we can start our constructor test.

We first define a class called X, define the constructor and copy constructor, in it, use the previous definition of the count:

Class X {Friend Class C; Public: C CT; X (): CT (* New C) {CT ;} // Constructor, initialization CT and starts to accumulate X (const X & o) {CT = O.ct; CT ; } // Copy Constructor, copy the results of CT in COPY Constructor to CT and accumulate};

let us start

Main () {x x; x a = x; x b = a; cout << B.ct.count; / * The number obtained is the number of times the object B's Constructor and Copy Constructors execute * /

Cout << x.ct.count; / * is the number of times the object X's Constructor and Copy Constructor execute * /}

Let's add a little code and do a test.

After Class X, we add a X function:

X func (x & _this) {return _this;}

Undoubtedly, this function will also call Copy Constructor, so how do you get his number of times?

Main () {x x; x a = x; x b = a; int i = func (b) .ct.count; cout << i; / * This i is constructor and copy constructor obtained after using FUNC functions The number of executions * /} Someone wants to ask, why should I write int i = func (b) .ct.count; not directly write: func (b) .ct.count;?

Because func is a function of type X, and this function is to call Copy Constructor, and this call is after return_this, so if you write directly: func (b) .ct.count; will be less accumulated, so Using an INT i will get a fully executed FUNC function call constructor and a Copy Constructionor.

In the X class, our friend Class C, CT initializes when the X object constructor is constructed, and then accumulate, the lifecycle of the CT will remain in the end of class X.

2002/6/24

转载请注明原文地址:https://www.9cbs.com/read-5988.html

New Post(0)