Reputation: 2261
I have C++/CLI code, which looks like:
interface class Type
{
};
public ref class Type1 : Type
{
public:
double a;
};
public ref class Type2 : Type
{
public:
double b;
};
generic <typename T> where T:Type
array<T>^ TestClass::TestFunction()
{
array<T>^ result = gcnew array<T>(1);
result[0].b = 10.0;
return result;
}
Type1
and Type2
have nothing in common, So I haven't kept anything in Type
to implement. Its just for generics syntax purpose, I have created Type
.
When I try to call TestFunction
using Type1
or Type2
, their members a
& b
are not accessible.
Compiler says, a
or b
is not a member of Type
.
Can you please tell me what wrong I am doing here?
Upvotes: 0
Views: 78
Reputation: 941237
resultBuffer[0].b = 10.0;
Best if you write code in a programming editor instead of an SO question box. You are just adding confusion, this at least needs to be result[0]->b = 10.0;
. But surely the real problem is that it is not generic code at all. As posted, it could only ever work if you write this as a template instead of a generic and then only ever use it with Type2 as the template argument. That is not useful either.
.NET generics require you to constrain the type argument so it always has a b
property so the method can always be generated at runtime for any T. So your interface must look like:
interface class IType
{
property double b { double get(); void set(double); };
};
Only the setter is actually required, I assumed you'd need the getter in practice. How you implement the interface in the classes that inherit the interface is something you need to think about, I can't make a reasonable guess.
Upvotes: 3