March 3rd, 2010 07:50 AM
#1
Interface operator bug or feature with implicit/explicit cast ?
An interface in C++/CLI can have operators as static functions of the interface. If I code an implicit cast operator for an interface the compiler accepts it:
// VC9ClrConsoleApplication.cpp : main project file.
#include "stdafx.h"
using namespace System;
interface class AnInterface
{
static operator int (AnInterface ^ af) { return af -> IntReturn(); }
int IntReturn();
};
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Console::ReadKey();
return 0;
}
This compiles with no errors.
If I change my operator from an implicit cast to an explicit cast I get a C2071 illegal storage class error:
// VC9ClrConsoleApplication.cpp : main project file.
#include "stdafx.h"
using namespace System;
interface class AnInterface
{
static explicit operator int (AnInterface ^ af) { return af -> IntReturn(); }
int IntReturn();
};
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Console::ReadKey();
return 0;
}
".\VC9ClrConsoleApplication.cpp(10) : error C2071: 'AnInterface: perator int' : illegal storage class".
I can so no reason why an implicit cast operator is allowed for an interface and an explicit cast operator is not allowed for an interface.
For an implicit cast operator one would be able to say:
AnInterface ^ ai = CreationFunctionFromClassImplementingInterface();
int i = ai;
For an explicit cast operator, if it worked with interfaces, one would be able to say:
AnInterface ^ ai = CreationFunctionFromClassImplementingInterface();
int i = static_cast<int>(ai);
Is this a bug or is it, for some reason I can not fathom, just the way C++/CLI is supposed to work.
Edward Diener
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks