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

  • SEARCH
  • Home
  • 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 7976889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:02:09+00:00 2026-06-04T09:02:09+00:00

Can someone please explain, in beginner terms, how operator overloading works? I need to

  • 0

Can someone please explain, in beginner terms, how operator overloading works? I need to overload the “+” operator as a member function with chaining to an objects array. Ive read some things from google, and it seems to operates the same way as “+” does without being overloaded, so I’m really confused.

This is what I have so far for the declaration:

ALIST<t_type> & operator+(const t_type );

So I declared this inside my ALIST class in the public section. Is the “+” operator overloaded now, or is there more I need to do in this functions definition to make it overloaded?

Also, once it is finally overloaded, what can I now do with it? What was the point of overloading it?

Please explain in beginner terms please, thanks.

  • 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-06-04T09:02:10+00:00Added an answer on June 4, 2026 at 9:02 am

    Here is an example for why.

    Say you are implementing a class of special vectors. For instance a Color class.
    Colors are going to basically be vectors with 4 values, r,g,b,a.

    Now we are writing a graphics engine and in our program we will often want to know what happens to a pixels when the light from two different sources act on a pixel we are evaulauting (as in a ray tracer). It would be convient for to define a + operator to evaulate what happens when the light from two different sources are added together.

    If we didnt have an operator you might write in your code to add two Colors together:

    Color newColor = Color(
        color1.r + color2.r, 
        color1.g + color2.g,
        color1.b + color2.b,
        color1.a + color2.a
    );
    

    Even worse, if you are working closely with the physics of the light you might find that colors dont add normally. They might for instance add according to some linear function e.g. f(a) = a^2 … (light doesn’t do this I don’t think, its just a random example).

    f(a) = a^2; f(b) = b^2
    f(a + b) = ??
    a = f(a)^.5; b = f(b)^.5
    a + b = f(a)^.5 + f(b)^.5
    f(a + b) = (f(a)^.5 + f(b)^.5)^2 *yada yada yada i'm terrible at math.
    

    Which means our Color adding code now becomes

    Color newColor = Color(
        pow(pow(color1.r, .5) + pow(color2.r, .5),2), 
        pow(pow(color1.g, .5) + pow(color2.g, .5),2),
        pow(pow(color1.b, .5) + pow(color2.b, .5),2),
        pow(pow(color1.a, .5) + pow(color2.a, .5),2), 
    );
    

    A pain to write out.
    But of course, If we take the Color class, and overwrite the add operator to do all of this for us, in our code we can just write

    Color color = color1 + color2;
    

    With this defined in out Color class definition

    Color Color::operator+(const Color &rhs) const {
         return Color(
               pow(pow(this.r, .5) + pow(rhs.r, .5),2), 
               pow(pow(this.g, .5) + pow(rhs.g, .5),2),
               pow(pow(this.b, .5) + pow(rhs.b, .5),2),
               pow(pow(this.a, .5) + pow(rhs.a, .5),2)
         ); 
    }
    

    Since our special adding code is in only one place, you can optimize it far better, and the code in the rest of your program becomes much more readable.

    That one way to looks at it at least. In the past, I have prefered functions like addLights(color1, color2) as this is easier to code, just as easy to read, and more readable since it is obvious it is not a traditional vector add. I bet you could go your entire career not overriding operators and I dont think you would not be missing out on much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain or give some resources on how function composition works in
Can someone please explain why the following doesn't work works: function displayResults(data) { $(#odNextPage).click(function()
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
Can someone please explain to me how this responsive approach works? This was done
Can someone please explain how static variables in member functions work in C++. Given
Can someone please explain in simple terms how the AND, SHL and CMP commands
Can someone please explain to me why the equality operator in PowerShell is not
Can someone please explain me why we need nested locking ? look at this
Can someone please explain to me why the hell? When I try to set
Can someone please explain this phenomenon? I am using Mojarra 2.1.6 - Glassfish 3.1.2.

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.