编译器错误 C3293
“accessor”:使用“default”访问“type”类的默认属性(索引器)
未正确访问索引的属性。 有关详细信息,请参阅如何:在 C++/CLI 中使用属性。
Visual Studio 2017 及更高版本:在 Visual Studio 2015 及更早版本中,编译器在某些情况下将默认属性误识别为默认索引器。 有可能通过使用标识符“default”访问该属性来解决这个问题。 在 C++11 中将“default”引入为关键字后,解决方法本身会出现问题。 因此,在 Visual Studio 2017 中,需要解决方法的 Bug 都已修复,现在将“default”用于访问类的默认属性时,编译器会引发错误。
示例
以下示例在 Visual Studio 2015 及更早版本中生成 C3293。
// C3293.cpp
// compile with: /clr /c
using namespace System;
ref class IndexerClass {
public:
// default indexer
property int default[int] {
int get(int index) { return 0; }
void set(int index, int value) {}
}
};
int main() {
IndexerClass ^ ic = gcnew IndexerClass;
ic->Item[0] = 21; // C3293 in VS2015 OK in VS2017
ic->default[0] = 21; // OK in VS2015 and earlier
String ^s = "Hello";
wchar_t wc = s->Chars[0]; // C3293 in VS2015 OK in VS2017
wchar_t wc2 = s->default[0]; // OK in VS2015 and earlier
Console::WriteLine(wc2);
}