在条件表达式中,使用运算符 auto_gcroot 。
operator bool() const;
返回值
如果指定的对象有效,则为 true;否则为 false。
备注
比 bool 安全的此运算符实际上转换为 _detail_class::_safe_bool,因为它无法转换为整型。
示例
// msl_auto_gcroot_operator_bool.cpp
// compile with: /clr
#include <msclr\auto_gcroot.h>
using namespace System;
using namespace msclr;
int main() {
auto_gcroot<String^> s;
if ( s ) Console::WriteLine( "s is valid" );
if ( !s ) Console::WriteLine( "s is invalid" );
s = "something";
if ( s ) Console::WriteLine( "now s is valid" );
if ( !s ) Console::WriteLine( "now s is invalid" );
s.reset();
if ( s ) Console::WriteLine( "now s is valid" );
if ( !s ) Console::WriteLine( "now s is invalid" );
}
要求
头文件 <msclr\auto_gcroot.h>
Namespace msclr