Modeller

Hi!

How can I overload operator[] or operator() for array scripting in Whidbey VC++.NET

Example : 

public value class MyFunction {

private :

   array<double> descreteValues;

public:

   MyFunction(int n)
   {
    descreteValues = (gcnew array<double>(n));
   }

   /*

      Overload the operator ( ) or [ ] for array scripting such that
      both left and right side operations will be possible

      e.g. for [ ] define a property such that
      Right =>  a[ i ] = value;  // (set)
      Left  => value =  a[ i ];   // (get)

   */

   /*   it is possible to do it in C# in the following manner :

      
       public double this[int index]
       {

          // if (0 <= index && index < descreteValues->Length)

            get
            {
              return descreteValues[ i ];
            }

            set
            {
              descreteValues [ i ] = value;
            }
       }

You can see this type of overloading here : http://www.codeproject.com/useritems/VectorType.asp, just scroll down to the subtitle : "Accessing the variables" then look at the bottom of the first codesnippet!

I could not manage it in VC++.NET!

   */
};
 

My question is as follows : How do I do it in VC++.NET Express Edition

Any link/reference/code snippet is GREATLY appreciated.

Thanks in advance.
M.

 



Re: Visual C++ Language Operator overloading in VC++.NET Express Edition

nobugz

Moved to C++ Language forum.





Re: Visual C++ Language Operator overloading in VC++.NET Express Edition

einaros

Here's one simple way to do it, in both managed and native C++:

#include <iostream>
#include <vector>

using namespace std;
using namespace System;
using namespace System::Collections::Generic;

class Foo
{
public:
Foo(size_t num)
: v(num)
{
}

double& operator[](size_t pos)
{
return v[pos];
}

private:
vector<double> v;
};

ref class ManagedFoo
{
public:
ManagedFoo(int num)
{
arr = gcnew array<double>(num);
}

double% operator[](int pos)
{
return arr[pos];
}

private:
array<double>^ arr;
};

int main()
{
Foo f(10);
cout << f[0] << endl;
f[0] = 20;
cout << f[0] << endl;

ManagedFoo mf(10);
Console::WriteLine(mf[0]);
mf[0] = 2.2;
Console::WriteLine(mf[0]);
}






Re: Visual C++ Language Operator overloading in VC++.NET Express Edition

Modeller

Tusen takk Einar Otto,

but the solution you provide is only for left operand, e.i.

mf[0] = 2.2; from your example.

The operator must be double sided, e.i. it must also provide the right operand. Thus the following assignment

double rightValue = m[0];

must assign rightValue to 2.2.

Now the question is : Will "this" pointer resolve the issue

in native :

double& this[](size_t pos)
{
return v[pos];
}

in managed :

double% this[](int pos)
{
return arr[pos];
}

Will it work

I thank you very much again.

Regards,

M.





Re: Visual C++ Language Operator overloading in VC++.NET Express Edition

einaros

Modeller wrote:

mf[0] = 2.2; from your example.

The operator must be double sided, e.i. it must also provide the right operand. Thus the following assignment

double rightValue = m[0];

With the subscript operator there's no such thing as being double-sided. The operator will either return a reference, pointer or value. If a reference (tracking reference in C++/CLI) or pointer is returned, the actual (referenced) value can be changed. If a value is returned, it's a *copy* of whatever operator[]'s body fetched, in which case only the copy will be changed if you assign anything to it (thus the term "assign to temporary", which is more often than not a meaningless thing to do). The value can be read both in case of the reference / pointer and value.

There's no reason to provide any other operators. Keep in mind that C++ (and C++/CLI) aren't equal to C#.






Re: Visual C++ Language Operator overloading in VC++.NET Express Edition

Modeller

OK! I'll take your word for it.

Thank you very much again.

Regards

M.