C++/cli object and trace reference based on stack resolution

Source: Internet
Author: User
Tags constructor reference

Assigning object instances on the managed heap appears to be using Managed Extensions C + +, C #, J #, VB. NET programmer, and programmers who use local C + + can not only allocate memory on the heap, but are even more accustomed to using a stack based object instance.

Now review the previously defined point reference class, and then look at the following variable definitions:

Point p1, p2(3,4);

From the point of view of local C + +, p1 and P2 should be based on the stack reference class point instance, even from a general standpoint. The P1 is initialized by the default constructor, and P2 is initialized by the constructor that accepts the x and Y coordinates. From an implementation standpoint, point is a self-contained type (i.e., it does not contain any pointers or handles), however, as an instance of a reference class, it is still under the control of the CLI runtime and, if necessary, is garbage-collected-because of this, it is not possible to define a static or global instance of a reference class.

Meanwhile, you also cannot apply sizeof to an expression that indicates that it is referencing a class instance because the sizeof is evaluated at compile time, and the point object is sized until run time, but the sizeof can be applied to a handle because its size is determined at compile time.

Also, you cannot define a stack-based CLI array instance.

Trace References

Local C + + can use & to define an object's alias, for example, for any local class N, you can write the following code:

N n1;
N& n2 = n1;

References must be initialized at definition time, and throughout life, they are locked in reference to the same object, that is, its value does not change. An instance referencing a reference class is basically consistent with referencing a local class, except that the syntax is different.

Instances of reference classes are "moved" in memory during program execution, so they need to be tracked while local pointers and references are not qualified for this work (especially if you cannot use the address character & for an instance of a reference class), so c++/ The CLI provides a handle and a reference for tracing--this is referred to as a trace reference (tracking References), for example, you can define a trace reference P3 to track the object P2:

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.