我在 C++/CLI 中遇到了一些与我认为我知道的相反的东西:
通常,如果你将一个对象传递给一个函数,你会使用一个点来访问它的方法(这也适用于ref类,带有一些额外的构造函数):
value class Value {
void Print() { Console::WriteLine("Value"); }
};
void f(Value v) {
v.Print();
}
通常,通过接口将对象传递给函数会强制您将 a^放在参数上,并->在方法调用中使用:
interface class Base {
void Print();
};
void f(Base ^b) {
b->Print();
}
但是,如果您f使用基于接口的约束进行泛型,编译器会坚持您使用->,但也坚持您不要^在参数列表中使用:
interface class Base {
void Print();
};
generic <class T> where T : Base
void f(T t) {
t->Print();
}
到目前为止,我认为直接引用对象总是使用.,通过句柄引用它们总是使用->. 这似乎直接使用 a 引用一个对象->- 我错了什么?