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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:07:26+00:00 2026-05-26T16:07:26+00:00

I was going through some book and I decided to write my own implementation

  • 0

I was going through some book and I decided to write my own implementation of post-increment operator for user defined type. Here is the code.

#include <iostream>

using namespace std;

class X
{
    int a;

    public:
    X(int x=1):a(x){}

    X operator++(int)
    {
        X oldobj = *this;
        (*this).a++;
        return oldobj;
    }

int get(){return a;}

};

int main()
{
    X obj,obj2;
    obj++ = obj2;
    cout<< obj.get() << endl;
    return 0;
}   

I would expect the output to be 1 since obj2’s value will be copied after the increment is done. But the output was 2.

Thoughts?

P.S. I know this code will not win any medals and its fallacies. It is just for my understanding. Incidentally, ++obj = obj2 returns 1;

Is the behavior undefined?

  • 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-26T16:07:27+00:00Added an answer on May 26, 2026 at 4:07 pm

    As your syntax tells you, the postfix operator returns a copy of the old value, so that’s what gets incremented, not your object.

    Basically,

    obj++ = obj2;
    

    Will do this:

    X tempObj = obj;
    obj ++;
    tempObj = obj2;
    

    You’re assigning obj2 to a temporary variable.

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

Sidebar

Related Questions

I'm using VS2010 and I'm going through some examples in my book and I'm
I am going through some examples in a book I have and I have
I was going through some Java tutorials in a book and ran across code
I'm going through some book which tries to teach android development. In the book,
Going through some of my older Delphi projects and upgrading them to D2009, as
I was going through some code and came across a scenario where my combobox
I'm going through some old stored procedures at work and constantly come across CASE
I was going through some code that I downloaded off the internet ( Got
I was going through some data structures and I noticed this as a time
I've been going through some good (seeming) resources for Rails tutorials, and will dutifully

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.