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 646961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:40:14+00:00 2026-05-13T21:40:14+00:00

I realize this is a basic question but I have searched online, been to

  • 0

I realize this is a basic question but I have searched online, been to cplusplus.com, read through my book, and I can’t seem to grasp the concept of overloaded operators. A specific example from cplusplus.com is:

// vectors: overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int,int);
    CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
  x = a;
  y = b;
}

CVector CVector::operator+ (CVector param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return (temp);
}

int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;
  cout << c.x << "," << c.y;
  return 0;
}

From http://www.cplusplus.com/doc/tutorial/classes2/ but reading through it I’m still not understanding them at all. I just need a basic example of the point of the overloaded operator (which I assume is the “CVector CVector::operator+ (CVector param)”).

There’s also this example from wikipedia:

 Time operator+(const Time& lhs, const Time& rhs)
 {
   Time temp = lhs;
   temp.seconds += rhs.seconds;
   if (temp.seconds >= 60)
   {
     temp.seconds -= 60;
     temp.minutes++;
   }
   temp.minutes += rhs.minutes;
   if (temp.minutes >= 60)
   {
     temp.minutes -= 60;
     temp.hours++;
   }
   temp.hours += rhs.hours;
   return temp;
 }

From “http://en.wikipedia.org/wiki/Operator_overloading“

The current assignment I’m working on I need to overload a ++ and a — operator.

Thanks in advance for the information and sorry about the somewhat vague question, unfortunately I’m just not sure on it at all.

  • 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-13T21:40:14+00:00Added an answer on May 13, 2026 at 9:40 pm

    Operator overloading is the technique that C++ provides to let you define how the operators in the language can be applied to non-built in objects.

    In you example for the Time class operator overload for the + operator:

    Time operator+(const Time& lhs, const Time& rhs);
    

    With that overload, you can now perform addition operations on Time objects in a ‘natural’ fashion:

    Time t1 = some_time_initializer;
    Time t2 = some_other_time_initializer;
    
    Time t3 = t1 + t2;    // calls operator+( t1, t2)
    

    The overload for an operator is just a function with the special name “operator” followed by the symbol for the operator being overloaded. Most operators can be overloaded – ones that cannot are:

    .  .*  :: and ?:
    

    You can call the function directly by name, but usually don’t (the point of operator overloading is to be able to use the operators normally).

    The overloaded function that gets called is determined by normal overload resolution on the arguments to the operator – that’s how the compiler knows to call the operator+() that uses the Time argument types from the example above.

    One additional thing to be aware of when overloading the ++ and -- increment and decrement operators is that there are two versions of each – the prefix and the postfix forms. The postfix version of these operators takes an extra int parameter (which is passed 0 and has no purpose other than to differentiate between the two types of operator). The C++ standard has the following examples:

    class X {
    public:
        X&   operator++();      //prefix ++a
        X    operator++(int);   //postfix a++
    };
    
    class Y { };
    
    Y&   operator++(Y&);        //prefix ++b
    Y    operator++(Y&, int);   //postfix b++
    

    You should also be aware that the overloaded operators do not have to perform operations that are similar to the built in operators – being more or less normal functions they can do whatever you want. For example, the standard library’s IO stream interface uses the shift operators for output and input to/from streams – which is really nothing like bit shifting. However, if you try to be too fancy with your operator overloads, you’ll cause much confusion for people who try to follow your code (maybe even you when you look at your code later).

    Use operator overloading with care.

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

Sidebar

Related Questions

I realize this question is pretty basic, but I'm really stuck. I have a
I realize this question has probably been asked numerous times, but I have not
I realize that this is probably a very basic question, but I have spent
I know this is a basic question, but I can't find other StackOverflow posts
I realize this is more of a hardware question, but this is also very
I realize this is probably a hopelessly newbie question, but what is the difference
I'm using MySQL, but I think this is a basic SQL question. I don't
I can't seem to find the answer to this question anywhere. I realize that
I have a relatively basic question but more than anything just need some clarity
This may seem like a very basic question but I did not seem to

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.