The static constructor of the instance parsing c++/cli

Source: Internet
Author: User
Tags constructor unique id

For some classes, it is best to have an initialization process when used for the first time in a program; when the program is no longer needed, it is best to do some finishing work, which is a very good class design habit.

Elicit questions

If this is the case, each instance of a type must have its unique ID. For example, a type of transaction that can be used to track each transaction during processing, or later for the auditor to view the data file, for discussion, where the ID is the number of signed integers starting from 0.

If you save a NextID value in memory and when each new instance is constructed, it's a good idea to increment it by 1, but, in order to keep the ID value unique throughout the execution of a program, you need to save this value at the end of each program and restore the value the next time the program starts running. But in standard C + +, there is no way to achieve this, in fact, the use of the standard CLI library is also no way to complete. However, there are several extension libraries in the CLI's. NET implementation that can accomplish this task.

Recurring problem

This time we used the point class again, because points with a unique ID are good for this topic. The program in Example 1 is output after the code:

Example 1:

using namespace System;
Point F(Point p) {
  return p;
}
int main()
{
  /*1*/ Point::TraceID = true;
  /*2*/ Point^ hp1 = gcnew Point;
  Console::WriteLine("hp1: {0}", hp1);
  /*3*/ hp1->Move(6,7);
  Console::WriteLine("hp1: {0}", hp1);
  /*4*/ Point^ hp2 = gcnew Point(3,4);
  Console::WriteLine("hp2: {0}", hp2);
  /*5*/ Point p1, p2(-1,-2);
  Console::WriteLine("p1: {0}, p2: {1}", %p1, %p2);
  /*6*/ p1 = F(p2);
  Console::WriteLine("p1: {0}", %p1);
}

Output:

hp1: [0](0,0)
hp1: [0](6,7)
hp2: [1](3,4)
p1: [2](0,0), p2: [3](-1,-2)
p1: [2](-1,-2)

Reads the next available ID value from a text file when the program starts running, and uses it to initialize the private static field in a point class. Initially, this file contains a value of zero.

Based on the value of the public static Boolean property Traceid, the string generated by the ToString function in point can optionally contain the ID of point and be prefixed as a [id]. If this property value is true, the ID prefix is included, otherwise it is not included. By default, this property value is set to False, so we set it to true in label 1.

In label 2, the default constructor is used to allocate the memory space for point and displays its ID of 0 and a value of (0,0). In label 3, the x and Y coordinates of point are modified by the move function, but this does not modify the ID of point, after all, it is still the same instance--except for a different value. Then, in label 4, a constructor that accepts two parameters is used to allocate the memory space for another point and displays its ID of 1 and a value of (3,4).

Two stack-based instances are created in label 5 and their IDs and values are displayed. When the third and fourth point are created, their IDs are 2 and 3, respectively.

In label 6, P1 is assigned a new value, however, P1 is still the same point before it, so its ID does not change.

The second time you run the program, the output is as follows:

hp1: [6](0,0)
hp1: [6](6,7)
hp2: [7](3,4)
p1: [8](0,0), p2: [9](-1,-2)
p1: [8](-1,-2)

As shown above, 4 new instances are assigned a sequential ID value and are distinct from the first execution, but the IDs 4 and 5 are missing. Note the definition of label 6 and function F, where the point argument is passed to this function, and a point is returned by value. Similarly, both of these are called to the copy constructor, and they "faithfully" create a new instance, and each new instance has a unique ID. Therefore, when P2 is passed by value, a temporary point with ID 4 is created, followed by the creation of a copy with an ID of 5 when the copy is returned by value, and two replicas are disposable. When the program ends, the next available ID to write to the file is 6, and the next time the program runs, this is the ID that the first point will use when allocating space.

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.