help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Odd g++ results


From: Jeffrey Holle
Subject: Re: Odd g++ results
Date: Sat, 27 Sep 2003 18:26:58 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624

This is likely because the order of execution of parameters in C/C++ shouldn't be relied on. The "operator <<" is just a function after all...

If one codes like:

int main()
{
  int a = count();
  int b = count();
  int c = count();

  cout << "first " << a
       << " second " << b
       << " third " << c
       << " \n";

}

The output is as you expected.


Randy Goldenberg wrote:
Hi.

From the very simple code below, I expected the output to
be:

first 1 second 2 third 3

Compiling with g++ on Solaris, that was what I got.
However, the same code compiled on Linux outputted:

first 3 second 2 third 1

Ideas?


#include <iostream>

using namespace std;

int count()
{ static int j = 0;
  j++;
return j; }
int main()
{
  cout << "first " << count()
       << " second " << count()
       << " third " << count()
       << " \n";

}






reply via email to

[Prev in Thread] Current Thread [Next in Thread]