DotNet Visual C: I create a operator + declared in C# to be used in C++

  • olafbaeyens / 206 / Wed, 03 Mar 2010 04:18:00 GMT / Comments (3)
  • I create a operator + in C#

    public static HistogramBuffer operator+(HistogramBuffer
    aHistogramBuffer1, HistogramBuffer aHistogramBuffer2) {
    HistogramBuffer Tmp=new HistogramBuffer(aHistogramBuffer1);
    for (int iX=0; iX<Tmp.NrOfValues; iX++) {
    Tmp[iX]=Tmp[iX]+aHistogramBuffer2[iX];
    }
    return Tmp;
    }

    Now in C# I can do this:

    HistogramBuffer Test1=new HistogramBuffer();
    HistogramBuffer Test2=new HistogramBuffer();

    Test2=Test2+Test1;

    But if I do this In C++

    HistogramBuffer __gc *Test1=__gc new HistogramBuffer();
    HistogramBuffer __gc *Test2=__gc new HistogramBuffer();

    Test2=Test2+Test1;

    Then I get this error: "error C2845: '+' : cannot perform pointer arithmetic
    on __gc pointer...."
    Any idea how to fix this?

    --
    http://www.skyscan.be

  • Keywords:

    create, operator, declared, c#, used, c++, dotnet, visual, .net

  • http://dotnet.itags.org/visual-c/71167/«« Last Thread - Next Thread »»

    1. --
      cody

      Freeware Tools, Games and Humour
      http://www.deutronium.de.vu || http://www.deutronium.tk
      "Olaf Baeyens" <olaf.baeyens...skyscan.be> schrieb im Newsbeitrag
      news:41b5c7a2$0$25047$ba620e4c...news.skynet.be...
      > I create a operator + in C#
      > public static HistogramBuffer operator+(HistogramBuffer
      > aHistogramBuffer1, HistogramBuffer aHistogramBuffer2) {
      > HistogramBuffer Tmp=new HistogramBuffer(aHistogramBuffer1);
      > for (int iX=0; iX<Tmp.NrOfValues; iX++) {
      > Tmp[iX]=Tmp[iX]+aHistogramBuffer2[iX];
      > }
      > return Tmp;
      > }
      > Now in C# I can do this:
      > HistogramBuffer Test1=new HistogramBuffer();
      > HistogramBuffer Test2=new HistogramBuffer();
      > Test2=Test2+Test1;
      > But if I do this In C++
      > HistogramBuffer __gc *Test1=__gc new HistogramBuffer();
      > HistogramBuffer __gc *Test2=__gc new HistogramBuffer();
      > Test2=Test2+Test1;
      > Then I get this error: "error C2845: '+' : cannot perform pointer
      arithmetic
      > on __gc pointer...."
      > Any idea how to fix this?
      > --
      > http://www.skyscan.be

      since Test1 and Test2 are not HistogramBuffers but pointers to a
      HistogramBuffer you have to write something like:

      Test2 = &((*Test2)+(*Test1));

      You dereference both Test2 and Test1, add them together and get the address
      of the result.

      But Iam not sure weather this will work :)

      cody | Mon, 31 Dec 2007 15:08:00 GMT |

    2. > since Test1 and Test2 are not HistogramBuffers but pointers to a
      > HistogramBuffer you have to write something like:
      > Test2 = &((*Test2)+(*Test1));
      > You dereference both Test2 and Test1, add them together and get the
      address
      > of the result.
      > But Iam not sure weather this will work :)
      It doesn't compile, I also tried many variations with typecasting.
      Anyway it looks very ugly if I have to do that this way.

      So for the C++ I added a Add()
      Like this:
      Test2->Add(Test1);

      While in C# I use
      Test2=Test2+Test1;

      Thanks for the reply. :-)

      --
      http://www.skyscan.be

      olafbaeyens | Mon, 31 Dec 2007 15:09:00 GMT |

    3. This works in VC 2005. Using * for __gc class instances made it almost
      semantically impossible to support this in the MC++ 1.x syntax.

      Ronald

      "Olaf Baeyens" <olaf.baeyens...skyscan.be> wrote in message
      news:41b6b741$0$25060$ba620e4c...news.skynet.be...
      >> since Test1 and Test2 are not HistogramBuffers but pointers to a
      >> HistogramBuffer you have to write something like:
      >>
      >> Test2 = &((*Test2)+(*Test1));
      >>
      >> You dereference both Test2 and Test1, add them together and get the
      > address
      >> of the result.
      >>
      >> But Iam not sure weather this will work :)
      >>
      > It doesn't compile, I also tried many variations with typecasting.
      > Anyway it looks very ugly if I have to do that this way.
      > So for the C++ I added a Add()
      > Like this:
      > Test2->Add(Test1);
      > While in C# I use
      > Test2=Test2+Test1;
      >
      > Thanks for the reply. :-)
      > --
      > http://www.skyscan.be

      ronaldlaeremansmsft | Mon, 31 Dec 2007 15:10:00 GMT |