473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with operator

Hello,

I have the following C++-Code:

Definition:

class Klasse
{
public:
Klasse operator+(Klasse Op);

public:
int X;
};

Implementation:

Klasse Klasse::operator+(Klasse Op)
{
Klasse Result;

Result.X = this->X + Op.X;

return Result;
};
I want to transfer it to C#, like this:

class Klasse
{
public int X;
public static Klasse operator +(Klasse Op) // 'static' is
obligatory!
{
MVector Result;
Result = new Result();

Result.X = this.X + Op.X; //<- but this doesn't work in
'static'!

return Result;
}
}

The C#-Code is not working, because:

1. operator-methods have to be declared as static
2. static methoden my not use 'this'.

Unfortunately I don't find a solution. Can anybody give me a hint?

Thanks,
Lars

Jun 26 '07 #1
2 1098
On Jun 26, 7:48 am, boe...@sf-systemtechnik.de wrote:

<snip>
The C#-Code is not working, because:

1. operator-methods have to be declared as static
2. static methoden my not use 'this'.

Unfortunately I don't find a solution. Can anybody give me a hint?
You need to make your operator overload take *two* instances, not just
one:

public static Klasse operator +(Klasse first, Klasse second)
{
MVector Result;
Result = new Result();

Result.X = first.X + second.X;

return Result;
}

(Did you mean to make the type of Result Klasse rather than either
Result or MVector, by the way?)

Jon

Jun 26 '07 #2
<bo****@sf-systemtechnik.dewrote in message
news:11**********************@u2g2000hsc.googlegro ups.com...
The C#-Code is not working, because:

1. operator-methods have to be declared as static
2. static methoden my not use 'this'.

Unfortunately I don't find a solution. Can anybody give me a hint?
Operator redefinition is different in C#. In C++ the operator is an
instance method and it operates between the "this" and the received
argument. In C#, the operators are static, and they receive as arguments the
two parameters on which they operate:

public static Klasse operator+(Klasse t1, Klasse t2)
{
Klasse result;
result = new Klasse();
result.X = t1.X + t2.X;
return result;
}
Jun 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
4
by: Andre Paim Lemos | last post by:
Hi, I'm having some compiler problems when I try to use make_heap(), push_heap() and pop_heap(). I am compiling my code on gcc version 3.3.1 (SuSE Linux). I am using the heap related methods to...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
6
by: Tony Johansson | last post by:
Hello Experts! I have the user defined class called Boolean that should be handle all kind of logic expression such as if (a && b && c) or if (!a && b && c) osv I have the class definition...
1
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
6
by: junw2000 | last post by:
Hi, I wrote a simple code about operator overloading. But it can not compile. Below is the code: #include <iostream> using namespace std;
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
2
by: Dark Wind | last post by:
Hi, I have been using OPT++ to solve a non linear programming problem. I am totally new to C++, but I looked at an example given on OPT++ website and modified it according to my problem. But I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.