C ++: packing the most powerful. NET Language

Source: Internet
Author: User

Reargument type

Before discussing boxing, it is necessary to figure out why the value type differs from the reference type.

What is the difference between an instance with a value type containing a number and an instance with a reference type pointing to an object? In addition to the memory required to store objects, each object has an object header to provide basic services for object-oriented programming, such as classes with virtual methods, embedded metadata. The memory overhead of the object header indirectly combined by Virtual Methods and interfaces is usually very high. Even if all you need is a static value, it will also bring some compiler forced operations. Interestingly, in some cases, the compiler can optimize some object overhead, but it does not always work. If you are very concerned about the execution efficiency of managed code, using numeric or value types will be helpful, but in the local C ++ type, this is not a big difference, of course, C ++ does not force any programming paradigm, so it is also possible to create such a different type system by creating a library on top of C ++.

  Packing

What is boxing )? Packing is a mechanism used to bridge between values and objects. Although each type of CLR is derived directly or indirectly from the Object class, the value is not. A value on a stack (such as an integer int) is a memory block that a compiler performs a specific operation. If you really want to regard a value as an Object, you must call the method inherited from the Object for the value. To achieve this, CLR provides the concept of packing. I know that the packing principle is still a bit useful. First, a numerical value is imported into the stack by using the ldloc IL command. Then, the IL command is run to increase the numerical type, CLR then releases the value from the stack and allocates enough space to store the value and the object header. Then, a reference to the new object is pushed into the stack. All these are the tasks to be done by the packing command. Finally, to get the object reference, the stloc IL command pops up the reference from the stack and stores it in a local variable.

Now, the question is: in programming languages, should the number packing operations be expressed as implicit or explicit? In other words, should an explicit conversion or other constructor be used? The C # Language designer decides to implement implicit conversion. After all, an integer is an Int32 type indirectly inherited from an Object.

Int I = 123;
Object o = I;

The problem arises. As we know, packing is not a simple upward conversion. It is a bit like converting a value into an object, which is an operation with a potential cost. For this reason, hosting C ++ uses the keyword _ box for explicit packing.

Int I = 123;
Object * o = _ box (I );

Of course, when hosting C ++, when packing a value, it will not lose static type information, which is exactly what C # lacks.

Int I = 123;
Int _ gc * o = _ box (I );

Specifying a strong packing value is conducive to conversion back to a numerical value type, or is called unboxing. Instead of using dynamic_cast, it simply references an object.

Int c = * o;

Of course, the syntactic cost of hosting C ++ explicit packing has proved to be huge in many cases. For this reason, the C ++/CLI language design process has changed to be consistent with C #-implicit packing. In the same case, it directly indicates that the type is safe in the strong-Type Packing value, which is exactly what other. NET languages cannot do.

Int I = 123;
Int ^ hi = I;
Int c = * hi;
Hi = nullptr;

Here, it also implies that a handle that does not point to any object cannot be initialized to zero. At this point, it is consistent with the pointer, this only causes the numeric value to be "zero" packed. This is also the reason for the existence of the constant nullptr. It can be assigned to any handle and is the equivalent of null keyword in C. Although nullptr is a new reserved word in the C ++/CLI language design, it has been proposed to be added to the Standard C ++ by Herb Suter and Bjarne Stroustrup.

Compile reference and value types

In C #, the keyword class is usually used to declare a reference type, while the keyword struct is used to declare the value type:

Class ReferenceType {}
Struct ValueType {}

For class and struct, C ++ has been defined, so this does not work in C ++. In the initial language design, the keyword _ gc placed before the class indicates that this is a reference type, while the keyword _ value indicates that this is a value type.

_ Gc class ReferenceType {};
_ Value class ValueType {};

C ++/CLI introduces the "null" keyword in areas that do not conflict with other user identifiers. To declare a reference type, you only need to add ref before class or struct. Similarly, you can use value to declare the value type.

Ref class ReferenceType {};
Ref struct ReferenceType {};

Value class ValueType {};
Value struct ValueType {};

The usage of class or struct is related to the visibility of class Members in the default state. The biggest difference in CLR is that only public inheritance is supported. Using private or protected inheritance will result in compilation errors. Therefore, explicitly declaring public inheritance is legal but redundant.

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.