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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:19+00:00 2026-05-23T09:46:19+00:00

Possible Duplicate: Undefined Behavior and Sequence Points I’m having trouble understanding the order of

  • 0

Possible Duplicate:
Undefined Behavior and Sequence Points

I’m having trouble understanding the order of actions when overloading the postfix operator. Let’s examine the two small examples below:

int i = 0;
std::cout << std::endl << "i: " << i;
i = ++i;
std::cout << std::endl << "i: " << i;
i = i++;
std::cout << std::endl << "i: " << i;

MyClass myObject;
std::cout << std::endl << "myObject: " << myObject.getMyValue();
myObject = ++myObject;
std::cout << std::endl << "myObject: " << myObject.getMyValue();
myObject = myObject++;
std::cout << std::endl << "myObject: " << myObject.getMyValue();

Two very different behaviors emerge. The output is as follows:

i: 0
i: 1
i: 2
myObject: 0
myObject: 1
myObject: 1

Different behavior, you see. Here’s the outline of my overloaded-operator methods.

MyClass & MyClass::operator++ ()
{
    ++myValue;
    return *this;
}

MyClass MyClass::operator++ (int postfixFlag)
{
    MyClass myTemp(*this);
    ++myValue;
    return myTemp;
}

Alright. Prefix makes sense. You increment whatever you need to, then return the same object, now modified, in case of assignment. But postfix is what’s tripping me up. It’s supposed to assign, then increment. Here we’re self assigning. So with the built-in integer type, it makes sense. I assign i‘s value to itself, then i gets incremented. Fair enough. But let’s say MyClass is a recreation of the int. It starts out at 0, gets prefix-incremented, and becomes 1. Then, the key line. myObject = myObject++. That’s the same thing as myObject = myObject.operator++(int postfixFlag). It gets called. myTemp gets initialized with the value 1. It’s incremented to 2. Then we return the temp. That works, if we’re assigning to another object. But here I’m self-assigning, so after the increment to 2, myObject is set equal to the returned temp object initialized with the initial value, and we’re back to 1! That makes sense. But it’s a fundamentally different behavior.

How do I work around it? How does int do it? How is this method generally written? Do you have any comments about C++ behavior and design relating to this? Etc. I’m a little perplexed right now, since books and online examples always seem to use a variant on the method above.

Thanks for reading, and any input will be appreciated!

  • 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-23T09:46:20+00:00Added an answer on May 23, 2026 at 9:46 am

    As others have said, with int the behaviour is undefined. But I thought I’d try to explain why for your MyClass it is not ever getting to 2.

    The trick is that you are taking the following three steps in the postfix version:

    1. Making a copy of this called myTemp (with myValue == 1).
    2. Incrementing this->myValue (so myTemp.myValue == 1; this->myValue == 2).
    3. Returning myTemp (with myValue == 1).

    So you are modifying this, but the code that calls myObject++ is never going to see this again. It’s only going to look at the value returned, which is a copy of the old myObject.

    The code for operator++ is fine. The problem is how you are using it — you shouldn’t be writing the result of a pre-increment or post-increment back to the same variable (behaviour is undefined). Here is some code that might be more instructive:

    int i = 0;
    std::cout << "i: " << i << std::endl;
    int j = ++i;
    std::cout << "i: " << i << ", j: " << j << std::endl;
    int k = i++;
    std::cout << "i: " << i << ", k: " << k << std::endl;
    
    MyClass myObject;
    std::cout << "myObject: " << myObject.getMyValue() << std::endl;
    MyClass myObject1 = ++myObject;
    std::cout << "myObject: " << myObject.getMyValue()
        << ", myObject1: " << myObject1.getMyValue() << std::endl;
    MyClass myObject2 = myObject++;
    std::cout << "myObject: " << myObject.getMyValue()
        << ", myObject2: " << myObject2.getMyValue() << std::endl;
    

    This prints:

    i: 0
    i: 1, j: 1
    i: 2, k: 1
    myObject: 0
    myObject: 1, myObject1: 1
    myObject: 2, myObject2: 1
    

    I changed your code so that rather than assigning back to itself, it assigns to a fresh variable each time. Note that in both the int and the MyClass cases, the main variable (i/myObject) is incremented both times. However, in the pre-increment case, the fresh variable (j/myObject1) takes on the new value, while in the post-increment case, the fresh variable (k/myObject2) takes on the old value.

    Edit: Just answering another part of the question, “How does int do it?” I assume this question means “what does the pre-increment and post-increment code look like in the int class, and how can I make mine the same?” The answer is, there is no “int class”. int is a special built-in type in C++ and the compiler treats it specially. These types aren’t defined with ordinary C++ code, they are hard-coded into the compiler.

    Note: For anyone who wants to try this themselves, here is the code for MyClass that the question didn’t include:

    class MyClass
    {
    private:
        int myValue;
    public:
        MyClass() : myValue(0) {}
        int getMyValue() { return myValue; }
        MyClass& operator++();
        MyClass operator++(int postfixFlag);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Undefined Behavior and Sequence Points after the following c++ code, the array
Possible Duplicate: Undefined Behavior and Sequence Points int a=10; a=a++; a=++a; a=(a++); Can you
Possible Duplicates: Undefined Behavior and Sequence Points Order of function call I have found
Possible Duplicate: Undefined Behavior and Sequence Points The variable i is changed twice, but
Possible Duplicate: FAQ : Undefined Behavior and Sequence Points Different outputs on different compiler?
Possible Duplicate: Undefined Behavior and Sequence Points I'm not sure if this is a
Possible Duplicate: Undefined Behavior and Sequence Points In C++ on a machine code level,
Possible Duplicate: Undefined behavior and sequence points What is the value of x after
Possible Duplicate: Undefined Behavior and Sequence Points What is the outcome of the following
Possible Duplicate: Undefined Behavior and Sequence Points Pleae explain the behaviour of following statements

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.