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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:07:33+00:00 2026-06-03T07:07:33+00:00

I have the following code: class Array { public: int aaa; Array():aaa(1){} void print()

  • 0

I have the following code:

class  Array
{  
   public: 
       int aaa;
       Array():aaa(1){}
      void print()
      {
          cout << aaa << endl;
      }

      Array& operator++()
      {
          aaa++;
          return *this;
      }
      Array operator++(int)
      {
          Array a(*this);
          aaa++;
          return a;
      }
};

I have some questions as follows:

  1. why prefix returns a reference and postfix returns an object? In the book C++ Primer, the author only explained “For consistency with the built-in operators“.

  2. Then, I tested the code:

    Array ar;

        (ar++).print(); // print 1
    
        ar.print(); // print 2
    

the output is exactly what I expected.
Now I changed the code in the overloading postfix function as:

Array operator++(int)
{
     Array a(*this);
     a.aaa++; // changed this
     return a;
}

I called the test code:

Array ar;
(ar++).print(); // this prints 2
ar.print(); // this prints 1

Why I got such results?

  • 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-06-03T07:07:34+00:00Added an answer on June 3, 2026 at 7:07 am
    1. The postfix operator returns an object, not a reference, because it has to return an unchanged version of the current object; it has to return the value before the increment is done. Therefore a new object must be allocated. If you returned a reference, what would it be a reference to?

    2. In your second example, you’re creating a new object, incrementing it, and returning it, but you’re not changing the original object that the operator was applied to — this is clearly wrong, so gives wrong results.

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

Sidebar

Related Questions

I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have encountered the following code: class a { public: void * operator new(size_t
I have following code public class TEST { public static void main(String arg[]){ try
I have following code in Java: import java.util.*; public class longest{ public static void
I have the following code: class outer { struct inner { int var1; int
I have the following code: class Program { static void Main(string[] args) { new
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called
I have following code that displays an Image with letters, public class MainActivity extends
I have the following code and json: public class Labels { public Labels() {}
So I have the following the code: namespace ConsoleApplication1 { public delegate int Transformer(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.