Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the Construction example of C++ object

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge points of this "C++ object Construction case Analysis" article are not quite understood by most people, so the editor summarizes the following, with detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "C++ object construction case analysis" article.

I. the construction of the object (part I) 1.1 the initial value of the object

Question: what is the initial value of the member variable in the object?

What are the initial values of the member variables I and j in the following class definition?

Let's take a look at the code for the initial value of a member variable:

# include class Test {private: int i; int j; public: int getI () {return I;} int getJ () {return j;}}; Test gt; int main () {printf ("gt.i =% d\ n", gt.getI ()); printf ("gt.j =% d\ n", gt.getJ ()); Test T1 Printf ("t 1.I =% d\ n", t1.getI ()); printf ("t 1.j =% d\ n", t1.getJ ()); Test* pt = new Test; printf ("pt- > I =% d\ n", pt- > getI ()); printf ("pt- > j =% d\ n", pt- > getJ ()); delete pt; return 0;}

The following is the output:

The storage space occupied by the object T1 is on the stack, and the member variables I and j do not have explicit initial values, so the initial values are uncertain. The storage space occupied by the object gt is in the global data area, so the initial value is 0.

Test* pt = new Test; means that a Test object is generated in the heap space, although pt- > I and pt- > j are both 0, which is just a coincidence, because when you create an object on the heap, the member variables are initially random values.

Note: classes actually get data types, so objects can be generated on the global data area, stack, and heap through this data type.

1.2 initialization of objects

From a programming point of view, objects are just variables, so:

When you create an object on the stack, the member variable is initially random.

When you create an object on the heap, the member variable is initially random.

When an object is created in a static store, the member variable is initially 0

All objects in life are listed after initialization.

The initial state (factory setting) is a common state of the object.

As far as the stock is concerned, the object needs a definite initial state.

Solution

Provide an initialize function of public in the class

Call the initialize function to initialize the object immediately after it is created

As follows:

Let's look at a piece of code that initializes the function:

# include class Test {private: int i; int j; public: int getI () {return I;} int getJ () {return j;} void initialize () {I = 1; j = 2;}}; Test gt; int main () {gt.initialize () Printf ("gt.i =% d\ n", gt.getI ()); printf ("gt.j =% d\ n", gt.getJ ()); Test T1; t1.initialize (); printf ("t 1.I =% d\ n", t1.getI ()); printf ("t1.j =% d\ n", t1.getJ ()); Test* pt = new Test; pt- > initialize () Printf ("pt- > I =% d\ n", pt- > getI ()); printf ("pt- > j =% d\ n", pt- > getJ ()); delete pt; return 0;}

The following is the output:

Existing problems

Initialize is just an ordinary function that must display the call

If the initialize function is not called, the running result is uncertain

Here is the solution:

Special member functions with the same class name can be defined in C++.

This special member function is called a constructor.

The construct does not have any declaration of the return type

The constructor is automatically called when the object is defined

Let's experience the constructor:

# include class Test {private: int i; int j; public: int getI () {return I;} int getJ () {return j;} Test () {printf ("Test () Begin\ n"); I = 1; j = 2; printf ("Test () End\ n") }; Test gt; int main () {printf ("gt.i =% d\ n", gt.getI ()); printf ("gt.j =% d\ n", gt.getJ ()); Test T1; printf ("t 1.I =% d\ n", t1.getI ()); printf ("t1.j =% d\ n", t1.getJ ()); Test* pt = new Test Printf ("pt- > I =% d\ n", pt- > getI ()); printf ("pt- > j =% d\ n", pt- > getJ ()); delete pt; return 0;}

The following is the output:

As you can see, Test () Begin and Test () End appear three times, that is, the constructor Test () is called three times because three objects are created.

1.3 Summary

Each object should be initialized before it is used

The constructor of the class is used for object initialization

The constructor has the same name as the class and has no return value

The constructor is automatically called when the object is defined

Second, the construction of objects (middle) 2.1 constructor

Constructor with parameters

The constructor can define parameters as needed

There can be multiple overloaded constructors in a class

The overloading of the constructor follows the rule of C++ overloading

As follows:

Friendly reminder

Object definition and object declaration are different

Object definition-apply for the space of the object and call the constructor

Object declaration-tells the compiler that such an object exists

As follows:

Automatic call of constructor

As follows:

Let's look at a piece of code for a constructor with parameters:

# include class Test {public: Test () {printf ("Test ()\ n");} Test (int v) {printf ("Test (int v), v =% d\ n", v);}}; int main () {Test t; / / call Test () Test T1 (1) / / call Test (int v) Test T2 = 2; / / call Test (int v) return 0;}

The following is the output, as expected.

One problem to be clear here is that int I = 1; is different from int i; i = 1;. The former is initialization, the latter is defined first, and then assigned. In the latter, the value of I is random because it is not initialized when I is defined. There is little difference between the two in C, but there is a big difference in C++. The difference is that in C++, the initialization calls the constructor. Let's take a look at an example, adding a line of code t = T2 to the above code

# include class Test {public: Test () {printf ("Test ()\ n");} Test (int v) {printf ("Test (int v), v =% d\ n", v);}}; int main () {Test t; / / call Test () Test T1 (1) / / call Test (int v) Test T2 = 2; / / call Test (int v) t = T2; return 0;}

The following is the output, which is exactly the same as the code output above. This is because initialization and assignment in C++ are different, initialization calls the constructor, but not assignment.

Let's look at another example:

# include class Test {public: Test () {printf ("Test ()\ n");} Test (int v) {printf ("Test (int v), v =% d\ n", v);}}; int main () {Test t; / / call Test () Test T1 (1) / / call Test (int v) Test T2 = 2; / call Test (int v) int I (100); printf ("I =% d\ n", I); return 0;}

The following is the output:

Call to the constructor

In general, the constructor is called automatically when the object is defined

-in some special cases, you need to call the constructor manually

Let's look at a piece of code that is called manually by the constructor:

# include class Test {private: int masked value; public: Test () {printf ("Test ()\ n"); m_value = 0;} Test (int v) {printf ("Test (int v), v =% d\ n", v) M_value = v;} int getValue () {return masked value;}}; int main () {Test ta [3] = {Test (), Test (1), Test (2)}; for (int I = 0; I < 3) GetValue +) {printf ("ta [% d] .getValue () =% d\ n", I, ta [I] .getValue ());} Test t = Test (100); printf ("t.getValue () =% d\ n", t.getValue ()); return 0;}

As you can see from the output below, Test (1), Test (2), and Test (100) are all manual calls to the constructor.

2.2 small instances

Requirements: develop an array class to solve the security problems of native arrays

Provide a function to get the length of the array

Provide a function to get array elements

Provides function settings array elements

IntArray.h:

# ifndef _ INTARRAY_H_ # define _ INTARRAY_H_class IntArray {private: int masks; int* masks; public: IntArray (int len); int length (); bool get (int index, int& value); bool set (int index, int value); void free ();}; # endif

IntArray.cpp:

# include "IntArray.h" IntArray::IntArray (int len) {m_pointer = new int [len]; for (int I = 0; I < len; iTunes +) {mpointern [I] = 0;} m_length = len;} int IntArray::length () {return masks;} bool IntArray::get (int index, int& value) {bool ret = (0)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report