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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:59:24+00:00 2026-05-26T08:59:24+00:00

In C++, wherever I see in web an example of suffix increment operator declaration,

  • 0

In C++, wherever I see in web an example of suffix increment operator declaration, it is always declared as

T& operator++(int);

and I believe this is the correct syntax of a suffix increment, isn’t it?

The issue is that, whenever I declare suffix increment, I declare return type with const keyword, so that it becomes lvalue-like.

Please see the example code:

class AClass
{
    int foo;

public:
    AClass(void) : foo(0) {};

    // Suffix increment operator
    // Consider adding const to return type
    /* const */ AClass operator++(int)
    {
        AClass cp(*this);
        foo++;
        return cp;
    };

    // Prefix increment operator
    AClass& operator++()
    {
        foo++;
        return *this;
    };
};

int main(int argc, const char* args[])
{
    /* This code would fail to compile.
    int bar = 5;
    (bar++)++;
     */

    // Similarily, I would expect this to fail
    //   but it will succeed unless I use const return type.
    AClass a;
    (a++)++;
}

I have never had problems about such a const-declared operator and I know it already saved our code from a bug made by a clumsy co-worker. So, my questions are:

  1. Are there any cons for such a practice? Is it a good practice indeed?
  2. What is the really correct declaration of suffix operator (I mean standards)?
  3. If this is not how the standard specifies but is already a good practice, shouldn’t it become a standard?

Thanks a lot for your answers!

  • 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-26T08:59:24+00:00Added an answer on May 26, 2026 at 8:59 am

    Suffix increment returns a temporary, not a reference (this means your first signature is wrong):

    T& operator++() // prefix
    {
        this->increment();
        return *this;
    }
    
    T operator++(int) // suffix
    {
        // Almost always, you'll have this code:
        T tmp(*this); ++(*this); return tmp;
    }
    

    Some people like to const-qualify the return value of the suffix operator to avoid writing stupid things like

    (a++).modify_me();
    

    which doesn’t modify a (it applies modify_me to a temporary object). Contrast with

    (++a).modify_me();
    

    which increments a and then modifies it.

    Personally, I don’t think it is necessary (since you may be interested in the side effects of modify_me). Moreover, in C++11, you may want to bind said temporary to a (non const) rvalue reference. Const qualifying the return type of suffix operators disables this possibility.

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

Sidebar

Related Questions

Well I have a question, See for example I have some web pages on
I want code to run whenever I create a new object. For example, see
Whenever I see images in an RSS feed, they are embedded in CDATA, rather
Whenever I see a problem that would be shared by others, with a solution
Whenever any question is asked, and a reference text is needed, I never see
All throughout an application wherever error messages (or other user messages) are used I
I am porting an existing application to C# and want to improve performance wherever
I've created a object that I'd like to have accessible from wherever the request-object
Whenever I design a database, I always wonder if there is a best way
I'm attempting to implement a Catalyst application using nginx as a frontend web proxy

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.