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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:05:29+00:00 2026-05-25T23:05:29+00:00

There is code: #include <iostream> class Int { public: Int() : x(0) {} Int(int

  • 0

There is code:

#include <iostream>

class Int {
public:
    Int() : x(0) {}
    Int(int x_) : x(x_) {}
    Int& operator=(const Int& b) {
        std::cout << "= from " << x << " = " << b.x << std::endl;
        x = b.x;
    }
    Int& operator+=(const Int& b) {
        std::cout << "+= from " << x << " + " << b.x << std::endl;
        x += b.x;
        return *this;
    }
    Int& operator++() {
        std::cout << "++ prefix " << x << std::endl;
        ++x;
        return *this;
    }
    Int operator++(int) {
        std::cout << "++ postfix " << x << std::endl;
        Int result(*this);
        ++x;
        return result;
    }
private:
    int x;

};

Int operator+(const Int& a, const Int& b) {
    std::cout << "operator+" << std::endl;
    Int result(a);
    result += b;
    return result;
}

int main() {
    Int a(2), b(3), c(4), d;
    d = ++a + b++ + ++c;
    return 0;
}

Result:

++ prefix 4
++ postfix 3
++ prefix 2
operator+
+= from 3 + 3
operator+
+= from 6 + 5
= from 0 = 11

Why postfix operator isn’t executed before prefix operator (++ prefix 4) altough priority of postfix operator is higher than prefix operator?

This was compiled by g++.

  • 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-25T23:05:30+00:00Added an answer on May 25, 2026 at 11:05 pm

    The order of evaluation of the different operands is unspecified which means that the compiler is free to reorder the evaluation of the ++a, b++ and ++c subexpressions as it pleases. The precedence of the operators does not really have any impact in that example.

    It does have an effect if you try to write ++i++ (where i is an int) which will be grouped as ++(i++) and it will fail to compile as the subexpression i++ is an rvalue and prefix increment requires an lvalue. If the precedence was reversed, then that expression would compile (and cause Undefined Behavior)

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

Sidebar

Related Questions

There is this code: #include <iostream> class Base { public: Base(){ std::cout << Constructor
1st code: #include <iostream> using namespace std; class demo { int a; public: demo():a(9){}
There is this code: #include <iostream> class SomeClass { int someInteger; float someFloat; public:
There is such code: #include <iostream> class A { public: int a; A() :
template<typename T> class ClassVariantVisitor : public boost::static_visitor<T> { public: T operator()(int& i) const {
I have this code: #include <iostream> using namespace std; class FooA { public: virtual
#include <iostream> #include <fstream> using namespace std; class Integer { public: int i; Integer
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
There is such code: #include <iostream> #include <vector> template <class T> class A{ public:
please consider following code #include <iostream> using namespace std; class Digit { private: int

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.