Standard C++ supports pointer types and null pointer constants. C++/CLI adds handle types and null values. 20
To help integrate handles, and to have a universal null, C++/CLI defines the keyword nullptr. This 21
keyword represents a literal having the null type. nullptr is referred to as the null value constant. (No 22
instances of the null type can ever be created, and the only way to obtain a null value constant is via this 23
keyword.) 24
The definition of null pointer constant (which Standard C++ requires to be a compile-time expression that 25
evaluates to zero) has been extended to include nullptr. The null value constant can be implicitly 26
converted to any pointer or handle type, in which case it becomes a null pointer value or null value, 27
respectively. This allows nullptr to be used in relational, equality, conditional, and assignment 28
expressions, among others. 29
Object^ obj1 = nullptr; // handle obj1 has the null value 30
String^ str1 = nullptr; // handle str1 has the null value 31
if (obj1 == 0); // false (zero is boxed and the two handles 32
differ) 33
if (obj1 == 0L); // false “ “ “ “ “ 34
if (obj1 == nullptr); // true 35
char* pc1 = nullptr; // pc1 is the null pointer value 36
if (pc1 == 0); // true as zero is a null pointer value 37
if (pc1 == 0L); // true “ “ “ 38
if (pc1 == nullptr); // true as nullptr is a null pointer constant 39
int n1 = 0; 40
n1 = nullptr; // error, no implicit conversion to int 41
if (n1 == 0); // true, performs integer comparison 42
if (n1 == 0L); // “ “ “ 43
if (n1 == nullptr); // error, no implicit conversion to int 44
if (nullptr); // error 45
if (nullptr == 0); // error, no implicit conversion to int 46
if (nullptr == 0L); // “ “ “ 47
nullptr = 0; // error, nullptr is not an lvalue 48
nullptr + 2; // error, nullptr can’t take part in arithmetic 49
Object^ obj2 = 0; // obj2 is a handle to a boxed zero 50
Object^ obj3 = 0L; // obj3 “ “ “ 51
String^ str2 = 0; // error, no conversion from int to String^ 52
String^ str3 = 0L; // “ “ “ “ 53
char* pc2 = 0; // pc2 is the null pointer value 54
char* pc3 = 0L; // pc3 “ “ “ 55
Language overview
15
Object^ obj4 = expr ? nullptr : nullptr; // obj4 is the null value 1
Object^ obj5 = expr ? 0 : nullptr; // error, no composite type 2
char* pc4 = expr ? nullptr : nullptr; // pc4 is the null pointer 3
value 4
char* pc5 = expr ? 0 : nullptr; // error, no composite type 5
6
int n2 = expr ? nullptr : nullptr; // error, no implicit conversion to 7
int 8
int n3 = expr ? 0 : nullptr; // error, no composite type 9
sizeof(nullptr); // error, the null type has no size, per se 10
typeid(nullptr); // error 11
throw nullptr; // error 12
void f(Object^); // 1 13
void f(String^); // 2 14
void f(char*); // 3 15
void f(int); // 4 16
f(nullptr); // error, ambiguous (1, 2, 3 possible) 17
f(0); // calls f(int) 18
void g(Object^, Object^); // 1 19
void g(Object^, char*); // 2 20
void g(Object^, int); // 3 21
g(nullptr, nullptr); // error, ambiguous (1, 2 possible) 22
g(nullptr, 0); // calls g(Object^, int) 23
g(0, nullptr); // error, ambiguous (1, 2 possible) 24
void h(Object^, int); 25
void h(char*, Object^); 26
h(nullptr, nullptr); // calls h(char*, Object^); 27
h(nullptr, 2); // calls h(Object^, int); 28
template<typename T> void k(T t); 29
k(0); // specializes k, T = int 30
k(nullptr); // error, can’t instantiate null type 31
k((Object^)nullptr); // specializes k, T = Object^ 32
k<int*>(nullptr); // specializes k, T = int* 33
Since objects allocated on the native heap do not move, pointers and references to such objects need not 34
track an object’s location. However, objects on the CLI heap can move, so they require tracking. As such, 35
native pointers and references are not sufficient for dealing with them. To track objects on the CLI heap, 36
C++/CLI defines handles (using the punctuator ^) and tracking references (using the punctuator %). 37
N* hn = new N; // allocate on native heap 38
N& rn = *hn; // bind ordinary reference to native object 39
R^ hr = gcnew R; // allocate on CLI heap 40
R% rr = *hr; // bind tracking reference to gc-lvalue 41
In general, the punctuator % is to ^ as the punctuator & is to *. 42
Just as Standard C++ has a unary & operator, C++/CLI provides a unary % operator. While &t yields a T* or 43
an interior_ptr<T> (see below), %t yields a T^. 44
Rvalues and lvalues continue to have the same meaning as with Standard C++, with the following rules 45
applying: 46
• An entity declared with type T*, a native pointer to T, points to an lvalue. 47
• Applying unary * to an entity declared with type T*, dereferencing a T*, yields an lvalue. 48
• An entity declared with type T&, a native reference to T, is an lvalue. 49
• The expression &lvalue yields a T*. 50
• The expression %lvalue yields a T^. 51
A gc-lvalue is an expression that refers to an object that might be on the CLI heap, or to a value member 52
contained within such an object. The following rules apply to gc-lvalues: 53
C++/CLI Language Specification
16
• Standard conversions exist from “cv-qualified lvalue of type T” to “cv-qualified gc-lvalue of 1
type T,” and from “cv-qualified gc-lvalue of type T” to “cv-qualified rvalue of type T.” 2
• An entity declared with type T^, a handle to T, points to a gc-lvalue. 3
• Applying unary * to an entity declared with type T^, dereferencing a T^, yields a gc-lvalue. 4
• An entity declared with type T%, a tracking reference to T, is a gc-lvalue. 5
• The expression &gc-lvalue yields an interior_ptr<T> (see below). 6
• The expression %gc-lvalue yields a T^. 7
The garbage collector is permitted to move objects that reside on the CLI heap. In order for a pointer to refer 8
correctly to such an object, the runtime needs to update that pointer to the object’s new location. An interior 9
pointer (which is defined using interior_ptr) is a pointer that is updated in this manner.
正文
Pointers, handles, and null2005-12-14 15:25:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/ainterly/8202.html
阅读(3176) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论