translate c# to c++

  • Thread starter Thread starter thwei
  • Start date Start date
T

thwei

How to translate this to c++

public new Bar this[int nIndex]

{

get{ return (Bar)base[nIndex]; }

}
 
thwei said:
How to translate this to c++

public new Bar this[int nIndex]

{

get{ return (Bar)base[nIndex]; }

}

In the new C++ syntax, this translates to (assuming Bar is a ref class):

property Bar^ default [int] {
Bar^ get(int nIndex) { return (Bar^)BaseClassName::default[nIndex]; }
}

In the old syntax, it would be something like this:

__property Bar* get_Item(int nIndex) {
return __try_cast<Bar*>(this->BaseClassName::Item[nIndex]; }
}

In the old syntax, you also need to put this attribute on the containing
__gc class: [System::Reflection::DefaultMember(S"Item")]
 
Assuming the class that contains that property derives from a class called
Bar.

[System::Reflection::DefaultMember(S"Item")]
public __gc class Bar
{
public:
__property Bar* get_Item(int index)
{
return NULL;
}
};

public __gc class Foo : public Bar
{
public:
__property Bar* get_Item(int index)
{
return Bar::get_Item(index);
}
};
 
U¿ytkownik "Hasani (remove nospam from address)"
Assuming the class that contains that property derives from a class called
Bar.

[System::Reflection::DefaultMember(S"Item")]
public __gc class Bar
{
public:
__property Bar* get_Item(int index)
{
return NULL;
}
};

public __gc class Foo : public Bar
{
public:
__property Bar* get_Item(int index)
{
return Bar::get_Item(index);
}
};

Yes, but this one problem:
public __gc class Bar
{
public:
__property Object* get_Item(int index)
{
return NULL;
}
};

public __gc class Foo : public Bar
{
public:
__property Bar* get_Item(int index) //Error C2392 covariant returns types
are not supported in managed types
{
return Bar::get_Item(index);
}
};


Exactly:

public __gc class Foo : public ArrayList
{
public:
__property Bar* get_Item(int index) //Error C2392 covariant returns types
are not supported in managed types
{
return Bar::get_Item(index);
}
};
 
In the old syntax, it would be something like this:
__property Bar* get_Item(int nIndex) {
return __try_cast<Bar*>(this->BaseClassName::Item[nIndex]; }
}

In the old syntax, you also need to put this attribute on the containing
__gc class: [System::Reflection::DefaultMember(S"Item")]

Type Item of base class is Object and this generated Error C2392 "covariant
returns types are not supported in managed types"
 
If you're intention is to derive Foo from ArrayList, you should instead
derive from CollectionBase. The problem is, an array list can store anything
derived from System.Object. In c#, you can use the 'new' keyword to overload
a method, but if your derived class is casted to an IList or an ArrayList,
nothing is to stop the user from calling IList[X] = Foo, or ArrayList[Y] =
Y.
thwei said:
U¿ytkownik "Hasani (remove nospam from address)"
Assuming the class that contains that property derives from a class called
Bar.

[System::Reflection::DefaultMember(S"Item")]
public __gc class Bar
{
public:
__property Bar* get_Item(int index)
{
return NULL;
}
};

public __gc class Foo : public Bar
{
public:
__property Bar* get_Item(int index)
{
return Bar::get_Item(index);
}
};

Yes, but this one problem:
public __gc class Bar
{
public:
__property Object* get_Item(int index)
{
return NULL;
}
};

public __gc class Foo : public Bar
{
public:
__property Bar* get_Item(int index) //Error C2392 covariant returns types
are not supported in managed types
{
return Bar::get_Item(index);
}
};


Exactly:

public __gc class Foo : public ArrayList
{
public:
__property Bar* get_Item(int index) //Error C2392 covariant returns types
are not supported in managed types
{
return Bar::get_Item(index);
}
};
 
Thanks. Its must derived over CollectionBase and work ok. Thanks again.

U¿ytkownik "Hasani (remove nospam from address)"
 
thwei said:
Type Item of base class is Object and this generated Error C2392
"covariant returns types are not supported in managed types"

Then the property type needs to be Object. The CLR doesn't support covariant
returns from virtual functions.
 
Back
Top