Overloading of [] operator ?

  • Thread starter Thread starter Gawel
  • Start date Start date
returntype LHStype::operator[](indextype index) {

public __gc class Class1
{
public:
Class1(void) {}
~Class1(void){}

int operator[](int index)
{
return 2;
}
};

And I got : PointFProba.h(44): error C3258: managed operators must be
declared using 'static op_<Name>' form ('operator' form is disallowed).
That is my problem :(

Gawel
 
// read-only, 1D
__property int get_List(int index){}

// write-only, 1D
__property void set_List(int index, int value){}

// read-only, 2D
__property int get_List(int index1, int index2){}

// write-only, 2D
__property void set_List(int index1, int index2, int value){}

It works. Thank you very much.

Gawel
 
One more question. If I write :

__property int get_List(int index){}

I can use it in MC++:

int i = o->List[1];

but in C# I don't have indexer but get_List method instead and I have to
write:

int i = o.get_List(1);

Well, how to implement indexer in MC++ that will be visible in C# ?

Gawel
 
Indexers and properties in C# are a little different, you don't use functions. You can overload indexers by the type of index used. This difference will cause things work a bit different

So as in C++ we call the indexer by its function name

Obj->myIndexer[2] = 10

In C# there's no function to call, so no "->" or "." are used

Obj[2] = 10

To call a C# indexer from C++, the name basically defaults to "Item", which you might have noticed in some of the controls. I think, but don't quote me on it, you can change this default name by using the runtime attribut

[System.Runtime.CompilerServices.IndexerName("NewIndexerName")
public bool this [ int index



You might be able to add this to C++ indexers, I haven't tried it yet, but since we've been talking about it, its got my brain ticking, so I'll more than likely have a go after I wake up.
 
Back
Top