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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:07:54+00:00 2026-06-01T02:07:54+00:00

In cpp, the result of the following code snippet is: 5 5 5 But

  • 0

In cpp, the result of the following code snippet is: 5 5 5
But in java, the result of the same code snippet is: 3 5 7
I do not know why, is there anyone could explain it?
Thanks a lot!

class H
{
  public:
    H &pr (int n, char * prompt)
    {
        cout<<prompt<<n<<" ";
        return *this;
    }

    H &fn(int n)
    {
        return pr(n,"");
    }
};

void test()
{
    int v=3;
    H h;
    h.fn(v).fn(v=5).fn((v=7));
}
  • 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-01T02:07:56+00:00Added an answer on June 1, 2026 at 2:07 am

    In cpp, the result of the following code snippet is: 5 5 5 But in java, the result of the same code snippet is: 3 5 7 I do not know why,

    Because C++ ain’t Java 🙂

    You are mutating the variable v in the last two function calls. Let’s look at the dissassembly (debug here to see things more clearly, in release a static value of 5 is used, but it could also be 7 just as easily. You’ll see why):

        h.fn(v).fn(v=5).fn((v=7));
    00411565  mov         dword ptr [v],7 
    0041156C  mov         dword ptr [v],5 
    00411573  mov         eax,dword ptr [v] 
    00411576  push        eax  
    

    The order of expression evaluation is not guaranteed to be the order that you call the functions here. You are modifying v between sequence points. 7 gets assigned to v, then 5, then the first function is called. Note that it doesn’t have to be 7 and then 5 in that order, it could be swapped! The order of evaluation is unspecified, it could be anything.

    You have a chain of functions which mutate v twice. You cannot count on the fact that each mutation will occur in the order you typed it here.

    We can simplify it. Let’s say we have two functions; x and y that both return an int. If I write:

    int k = x() + y();
    

    There is no guarantee that x() will be called before y(). So, if you are mutating an argument common to both functions then the mutation may occur in the call to y() first, which is what you are seeing.

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

Sidebar

Related Questions

I have the following CPP code snippet and the associated error message: Code snippet
I'm trying to execute this code nmea2kml.cpp but I keep getting the following errors.
I have the following code. // mfc.cpp : Defines the entry point for the
Why does the following code not give me a duplicate symbol linker error for
The following code compiles fine. but when goes to linking, it shows following error
The following code will cause compile errors in g++4.4: // File test.cpp namespace A
I have the following C++ code snippet (the C++ part is the profiler class
If I compile the following code: // // g++ static.cpp -o static.o // ar
I am consuming a cpp COM object from c# code. My c# code looks
I am attempting to compile the following template based code in VC++ 2005. #include

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.