|
-
February 24th, 2010, 08:29 PM
#1
C++/CLI operators in ref classes and interface classes
In C++/CLI if I add an operator to an interface class and implement that interface in a ref class, the operator is not recognized for the ref class. But if I add an operator to a base ref class and inherit from that base ref class in a derived ref class, the operator is recognized for that derived ref class:
Code:
#include "stdafx.h"
using namespace System;
interface class Base
{
public:
static int operator * (Base ^ it) { return 3; }
};
ref class Derived : Base
{
};
int main(array<System::String ^> ^args)
{
Derived ^ d(gcnew Derived);
int amt = *d;
Console::WriteLine(L"Hello World");
return 0;
}
The above compiles with an error of:
error C2440: 'initializing' : cannot convert from 'Derived' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
But if one changes the line in the code above from 'interface class base' to 'ref class base', the code compiles and runs with no errors.
Does anybody know why this behavior occurs ?
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
|