Share via


用户定义的转换 (C++/CLI)

本部分介绍当转换中的其中一种类型为值类型或引用类型的引用或实例时的用户定义的转换 (UDC)。

隐式转换和显式转换

用户定义的转换可以是隐式转换,也可以是显式转换。 如果转换不会导致信息丢失,则 UDC 应为隐式。 否则应定义显式 UDC。

本机类的构造函数可用于将引用或值类型转换为本机类。

有关转换的详细信息,请参阅装箱标准转换

// mcpp_User_Defined_Conversions.cpp
// compile with: /clr
#include "stdio.h"
ref class R;
class N;

value class V {
   static operator V(R^) {
      return V();
   }
};

ref class R {
public:
   static operator N(R^);
   static operator V(R^) {
      System::Console::WriteLine("in R::operator N");
      return V();
   }
};

class N {
public:
   N(R^) {
      printf("in N::N\n");
   }
};

R::operator N(R^) {
   System::Console::WriteLine("in R::operator N");
   return N(nullptr);
}

int main() {
   // Direct initialization:
   R ^r2;
   N n2(r2);   // direct initialization, calls constructor
   static_cast<N>(r2);   // also direct initialization

   R ^r3;
   // ambiguous V::operator V(R^) and R::operator V(R^)
   // static_cast<V>(r3);
}

输出

in N::N
in N::N

转换自运算符

convert-from 运算符可为从其他类的对象中定义运算符的类创建对象。

标准 C++ 不支持 convert-from 运算符;标准 C++ 为此使用构造函数。 但是,使用 CLR 类型时,Visual C++ 为调用 convert-from 运算符提供语法支持。

若要与其他符合 CLS 的语言进行良好互操作,你可能希望使用相应的 convert-from 运算符为给定类包装每个用户定义的一元构造函数。

convert-from 运算符:

  • 应定义为静态函数。

  • 可以是隐式的(如短格式转换为 int 等不损失精度的转换),也可以是显式的(可能损失精度)。

  • 应返回所含类的对象。

  • 应将“from”类型作为唯一参数类型。

以下示例演示了一个隐式和显式“convert-from”的用户定义的转换 (UDC) 运算符。

// clr_udc_convert_from.cpp
// compile with: /clr
value struct MyDouble {
   double d;

   MyDouble(int i) {
      d = static_cast<double>(i);
      System::Console::WriteLine("in constructor");
   }

   // Wrap the constructor with a convert-from operator.
   // implicit UDC because conversion cannot lose precision
   static operator MyDouble (int i) {
      System::Console::WriteLine("in operator");
      // call the constructor
      MyDouble d(i);
      return d;
   }

   // an explicit user-defined conversion operator
   static explicit operator signed short int (MyDouble) {
      return 1;
   }
};

int main() {
   int i = 10;
   MyDouble md = i;
   System::Console::WriteLine(md.d);

   // using explicit user-defined conversion operator requires a cast
   unsigned short int j = static_cast<unsigned short int>(md);
   System::Console::WriteLine(j);
}

输出

in operator
in constructor
10
1

convert-to 运算符

convert-to 运算符可为其运算符定义为其他对象的类转换对象。 以下示例演示了隐式的 convert-to 用户定义的转换运算符:

// clr_udc_convert_to.cpp
// compile with: /clr
using namespace System;
value struct MyInt {
   Int32 i;

   // convert MyInt to String^
   static operator String^ ( MyInt val ) {
      return val.i.ToString();
   }

   MyInt(int _i) : i(_i) {}
};

int main() {
   MyInt mi(10);
   String ^s = mi;
   Console::WriteLine(s);
}

输出

10

显式用户定义的 convert-to 转换运算符适用于可能会以某种方式丢失数据的转换。 若要调用显式 convert-to 运算符,必须使用强制转换。

// clr_udc_convert_to_2.cpp
// compile with: /clr
value struct MyDouble {
   double d;
   // convert MyDouble to Int32
   static explicit operator System::Int32 ( MyDouble val ) {
      return (int)val.d;
   }
};

int main() {
   MyDouble d;
   d.d = 10.3;
   System::Console::WriteLine(d.d);
   int i = 0;
   i = static_cast<int>(d);
   System::Console::WriteLine(i);
}

输出

10.3
10

转换泛型类

可以将泛型类转换为 T。

// clr_udc_generics.cpp
// compile with: /clr
generic<class T>
public value struct V {
   T mem;
   static operator T(V v) {
      return v.mem;
   }

   void f(T t) {
      mem = t;
   }
};

int main() {
   V<int> v;
   v.f(42);
   int i = v;
   i += v;
   System::Console::WriteLine(i == (42 * 2) );
}

输出

True

转换构造函数选择一个类型,并使用它来创建对象。 转换构造函数仅通过直接初始化进行调用;强制转换不会调用转换构造函数。 默认情况下,对于 CLR 类型,转换构造函数是显式的。

// clr_udc_converting_constructors.cpp
// compile with: /clr
public ref struct R {
   int m;
   char c;

   R(int i) : m(i) { }
   R(char j) : c(j) { }
};

public value struct V {
   R^ ptr;
   int m;

   V(R^ r) : ptr(r) { }
   V(int i) : m(i) { }
};

int main() {
   R^ r = gcnew R(5);

   System::Console::WriteLine( V(5).m);
   System::Console::WriteLine( V(r).ptr);
}

输出

5
R

在此代码示例中,隐式静态转换函数与显式转换构造函数执行相同的操作。

public value struct V {
   int m;
   V(int i) : m(i) {}
   static operator V(int i) {
      V v(i*100);
      return v;
   }
};

public ref struct R {
   int m;
   R(int i) : m(i) {}
   static operator R^(int i) {
      return gcnew R(i*100);
   }
};

int main() {
   V v(13);   // explicit
   R^ r = gcnew R(12);   // explicit

   System::Console::WriteLine(v.m);
   System::Console::WriteLine(r->m);

   // explicit ctor can't be called here: not ambiguous
   v = 5;
   r = 20;

   System::Console::WriteLine(v.m);
   System::Console::WriteLine(r->m);
}

输出

13
12
500
2000

另请参阅

类和结构