Concrete form constructor in C #

I study ICT. One of my courses is C #, and the other is physics. Our physics teacher used Visual Studio to animate some of the movements, gave us some of the code that he used to do this. He told us to take a look. There is something I don’t recognize:

public static Vector operator + (Vector v1, Vector v2) { return new Vector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z); } 

It should be a constructor, but I had never seen anything like it before. I don’t know what it's called, so I don’t know what to do on Google.

Can someone enlighten me please?

+4
source share
8 answers

This is not a constructor. This is the operator statement for the + operator. It determines what happens when you do something like the following: -

 var Vector v1 = new Vector(1,2,3); var Vector v2 = new Vector(1,2,3); var Vector v3 = v1 + v2; 
+4
source

This is called "operator overload." This is not a constructor, but returns a new vector.

See: Operator Overload

+10
source

It overloads the + operator for the Vector class. See here for more information on this topic.

+3
source

This is an overload of the “+” operator for the Vector class, so whenever you do “v1 + v2”, this code runs.

+3
source

This is not a constructor, but operator overloading for + Here is how you overload the behavior for:

 Vector a = new Vector(); Vector b = new Vector(); Vector c = a + b; 

See the MSDN article for more information.

+2
source

Line:

public static vector vector + (vector v1, vector v2)

... is not a constructor. It simply overloads the + (Plus) operator for the parent class. I assume the parent class is a refined vector class?

Line:

return a new vector (v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);

... Uses the constructor of the Vector class to pass three arguments (X, Y, and Z look like this)

0
source

This is an overloaded + operator. All OOP languages ​​(except Java may need to check the link) allow operator overloading.

0
source

It really overloads the + operator.

what does it mean? ok it's simple:

if you have this:

 int i = 10; int j = 20; int x = i + j; 

In this example, x will be 30, because C # knows if we use integers and use + that we need to take the sum.

But now you are working with Vectors. A 3d vector has 3 meanings: X, Y, and Z. If you want a sum of 2 vectors to work well? it looks like

 v1.X + v2.Y and v1.Y + v2.Z and v1.Z + v2.X 

or should C # do it like this:

 v1.X + v1.Y and v1.Z + v2.X and v2.Y + v2.Z 

When overloading an operator, you determine how the + operator should be implemented when using vectors. In our case, it is:

 v1.X + v2.X and v1.Y + v2.Y and v1.Z + v2.Z 

not so difficult;)

0
source

All Articles