Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6111493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:38:12+00:00 2026-05-23T14:38:12+00:00

I am trying to do operator overloads for += , but I can’t. I

  • 0

I am trying to do operator overloads for +=, but I can’t. I can only make an operator overload for +.

How come?

Edit

The reason this is not working is that I have a Vector class (with an X and Y field). Consider the following example.

vector1 += vector2;

If my operator overload is set to:

public static Vector operator +(Vector left, Vector right)
{
    return new Vector(right.x + left.x, right.y + left.y);
}

Then the result won’t be added to vector1, but instead, vector1 will become a brand new Vector by reference as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:38:13+00:00Added an answer on May 23, 2026 at 2:38 pm

    Overloadable Operators, from MSDN:

    Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded.

    Even more, none of assignment operators can be overloaded. I think this is because there will be an effect for the Garbage collection and memory management, which is a potential security hole in CLR strong typed world.

    Nevertheless, let’s see what exactly an operator is. According to the famous Jeffrey Richter’s book, each programming language has its own operators list, which are compiled in a special method calls, and CLR itself doesn’t know anything about operators. So let’s see what exactly stays behind the + and += operators.

    See this simple code:

    Decimal d = 10M;
    d = d + 10M;
    Console.WriteLine(d);
    

    Let view the IL-code for this instructions:

      IL_0000:  nop
      IL_0001:  ldc.i4.s   10
      IL_0003:  newobj     instance void [mscorlib]System.Decimal::.ctor(int32)
      IL_0008:  stloc.0
      IL_0009:  ldloc.0
      IL_000a:  ldc.i4.s   10
      IL_000c:  newobj     instance void [mscorlib]System.Decimal::.ctor(int32)
      IL_0011:  call       valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Addition(valuetype [mscorlib]System.Decimal,
                                                                                                    valuetype [mscorlib]System.Decimal)
      IL_0016:  stloc.0
    

    Now lets see this code:

    Decimal d1 = 10M;
    d1 += 10M;
    Console.WriteLine(d1);
    

    And IL-code for this:

      IL_0000:  nop
      IL_0001:  ldc.i4.s   10
      IL_0003:  newobj     instance void [mscorlib]System.Decimal::.ctor(int32)
      IL_0008:  stloc.0
      IL_0009:  ldloc.0
      IL_000a:  ldc.i4.s   10
      IL_000c:  newobj     instance void [mscorlib]System.Decimal::.ctor(int32)
      IL_0011:  call       valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Addition(valuetype [mscorlib]System.Decimal,
                                                                                                    valuetype [mscorlib]System.Decimal)
      IL_0016:  stloc.0
    

    They are equal! So the += operator is just syntactic sugar for your program in C#, and you can simply overload + operator.

    For example:

    class Foo
    {
        private int c1;
    
        public Foo(int c11)
        {
            c1 = c11;
        }
    
        public static Foo operator +(Foo c1, Foo x)
        {
            return new Foo(c1.c1 + x.c1);
        }
    }
    
    static void Main(string[] args)
    {
        Foo d1 =  new Foo (10);
        Foo d2 = new Foo(11);
        d2 += d1;
    }
    

    This code will be compiled and successfully run as:

      IL_0000:  nop
      IL_0001:  ldc.i4.s   10
      IL_0003:  newobj     instance void ConsoleApplication2.Program/Foo::.ctor(int32)
      IL_0008:  stloc.0
      IL_0009:  ldc.i4.s   11
      IL_000b:  newobj     instance void ConsoleApplication2.Program/Foo::.ctor(int32)
      IL_0010:  stloc.1
      IL_0011:  ldloc.1
      IL_0012:  ldloc.0
      IL_0013:  call       class ConsoleApplication2.Program/Foo ConsoleApplication2.Program/Foo::op_Addition(class ConsoleApplication2.Program/Foo,
                                                                                                              class ConsoleApplication2.Program/Foo)
      IL_0018:  stloc.1
    

    Update:

    According to your Update – as the @EricLippert says, you really should have the vectors as an immutable object. Result of adding of the two vectors is a new vector, not the first one with different sizes.

    If, for some reason you need to change first vector, you can use this overload (but as for me, this is very strange behaviour):

    public static Vector operator +(Vector left, Vector right)
    {
        left.x += right.x;
        left.y += right.y;
        return left;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to overload the dereference operator, but compiling the following code results in
I am trying to overload the stream insertion operator so can I print std::vector
Im trying to overload the [] operator in c++ so that I can assign
I am trying to overload the << operator, but I get the following error:
I'm trying to write a class that overloads the insertion operator but in my
I am trying to write operator overload for custom class and don't know how
I'm trying to create an overridden operator function using both const parameters, but I
I know of the ||= operator, but don't think it'll help me here...trying to
Ok, I am working through a book and trying to learn C++ operator overloading.
I am trying to overload the assignment operator to do a deep copy of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.