C + +: the most powerful. NET language Boxing

Source: Internet
Author: User
Tags header inheritance numeric numeric value
Re-discussion type

Before you discuss boxing (boxing), it's important to figure out why the value type differs from the reference type.

What is the difference between an instance of a value type that contains numeric values, and an instance of a reference type that points to an object? In addition to the memory required to store objects, each object has an object header that provides basic services for object-oriented programming, such as classes with virtual methods, metadata embedded in them, and so on. Object headers, which are indirectly bound by virtual methods and interfaces, typically have a large memory overhead, even if all you need is a static type of numeric value, which can also bring some compiler coercion. Interestingly, in some cases, the compiler can optimize the cost of some objects, but it does not always work. If you are very concerned about the efficiency of managed code execution, it would be useful to use numeric or value types, but this is not a big difference in the type of local C + +, of course, C + + does not enforce any programming paradigm, so it is possible to build a library to create such a distinct type system over C + +.

Packing

What is boxing (boxing)? Boxing is a mechanism used to bridge values and objects. Although each type of CLR derives directly or indirectly from the object class, the value is not. A value on a stack, such as an int, is just a block of memory that a compiler will perform a particular operation on. If you really want to think of a value as an object, you must invoke a method inherited from object for numeric values, in order to do this, the CLR provides a boxed concept. The idea of a little boxing is still a bit of a use, first, a value is put into the stack by using the ldloc il instruction, next, the boxed IL instruction runs, the value type is raised, the CLR pushes the value out of the stack, and allocates enough space to store the value and the object header, and then a reference to the new object is pushed onto the stack, All this is what the boxing instructions do. Finally, to get an object reference, the stloc il instruction pops the reference from the stack and stores it in a local variable.

The question now is: in the programming language, is the boxing operation of numeric values supposed to be implicit or explicit? In other words, should you use an explicit conversion or another constructor? The C # language designer decides to make an implicit conversion, after all, an integer is a Int32 type indirectly inherited from object.

int i = 123;
Object o = i;
The problem is, as we know, boxing is not a simple upward conversion, it's kind of like converting a number to an object, a potentially costly operation. It is for this reason that managed C + + uses the keyword __box for explicit boxing.

int i = 123;
object* o = __box (i);
Of course, in Managed C + +, when a value is boxed, static type information is not lost, which is what C # lacks.

int i = 123;
int __gc* o = __box (i);
Specifying a strongly typed boxed value facilitates conversion back to a numeric type, or is called a unpacking (unboxing), and does not use dynamic_cast, but simply dereference an object.

int c = *o;
Of course, the syntactic cost of hosting C + + 's explicit boxing has proved to be huge in many cases. Because of this, changed the design process of the C++/CLI language, became consistent with C #--implicit boxing. In the same case, it maintains type safety on direct representations of strongly typed boxed values, which is the other. NET language can not do.

int i = 123;
int^ hi = i;
int c = *hi;
hi = nullptr;
Here, too, implies a handle that does not point to any object, cannot be initialized to zero, at this point, it is consistent with the pointer, because this will cause the value "0" to be boxed only, and this is also the reason for the existence of the constant nullptr, which can be assigned to any handle and is a null equivalent of the keyword in C #. Although Nullptr is a new reserved word in the C++/cli language design, it has been added to standard C + + by Herb Sutter and Bjarne Stroustrup proposals.

Writing references and value types

In C #, a reference type is typically declared with the keyword class, and the value type is declared with the keyword struct: class Referencetype {}
struct ValueType {}
There is already a defined meaning for class and struct,c++, so this is not feasible in C + +. In the original language design, the keyword __gc placed in front of the class indicates that it is a reference type, while the keyword __value indicates that it is a value type.

__gc class Referencetype {};
__value class ValueType {};
C++/CLI introduces the "spacer" keyword where there is no conflict with other identifiers of the user. To declare a reference type, simply precede class or struct with ref, and similarly, use value to declare the value type.

Ref class Referencetype {};
Ref struct Referencetype {};

Value class ValueType {};
Value struct ValueType {};
With regard to using class or struct, it is related to the visibility of class members in the default state, and the biggest difference in the CLR is that only public inheritance is supported. Using private (private) or protected (protected) inheritance will result in compilation errors, so explicitly declaring public inheritance is legal but superfluous.

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.