A Qustion about iostream

Discussion in 'C++' started by want.to.be.professer, Aug 29, 2008.

  1. Yesterday, my friend ask me a question about iostream.I know the
    result ,but can't explain.The program is below:

    ---------------------------------------------------------------------------------------------------------------------------------------------------
    #include <iostream>

    using namespace std;

    int fun( int x )
    {
    cout << "____in fun()____" << endl;
    return x;
    }

    int main()
    {
    int x = 9;
    cout << "b = " << x << fun2( 2 ) << "ads" << endl;
    return 0;
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------------
    The program run as below:

    1.function "fun()"
    2.operator << ( basic_iostream<char, _Traits>& _Ostr, const char*
    _Val )
    ( _Val== "b = " )
    3. a member function : operator<<(int _Val)
    ( _Val == 9 )
    4. a member function : operator << ( int _Val )
    ( _Val == 2 )
    5. operator << ( basic_iostream<char, _Traits>& _Ostr, const char*
    _Val )
    ( _Val== "ads" )

    But why it run like this? I can't explain.So I put in here to wait a
    answer.
    want.to.be.professer, Aug 29, 2008
    #1
    1. Advertising

  2. On 8ÔÂ29ÈÕ, ÉÏÎç9ʱ44·Ö, Sam <> wrote:
    > want.to.be.professer writes:
    > > Yesterday, my friend ask me a question about iostream.I know the
    > > result ,but can't explain.The program is below:

    >
    > > ---------------------------------------------------------------------------------------------------------------------------------------------------
    > > #include <iostream>

    >
    > > using namespace std;

    >
    > > int fun( int x )
    > > {
    > > cout << "____in fun()____" << endl;
    > > return x;
    > > }

    >
    > > int main()
    > > {
    > > int x = 9;
    > > cout << "b = " << x << fun2( 2 ) << "ads" << endl;
    > > return 0;
    > > }

    >
    > > -------------------------------------------------------------------------------------------------------------------------------------------------------
    > > The program run as below:

    >
    > > 1.function "fun()"
    > > 2.operator << ( basic_iostream<char, _Traits>& _Ostr, const char*
    > > _Val )
    > > ( _Val== "b = " )
    > > 3. a member function : operator<<(int _Val)
    > > ( _Val == 9 )
    > > 4. a member function : operator << ( int _Val )
    > > ( _Val == 2 )
    > > 5. operator << ( basic_iostream<char, _Traits>& _Ostr, const char*
    > > _Val )
    > > ( _Val== "ads" )

    >
    > > But why it run like this? I can't explain.So I put in here to wait a
    > > answer.

    >
    > Because the C++ standard does not specify the evaluation order for
    > individual parts of an expression. A C++ compiler is free to choose any
    > evaluation order. Consider a simpler example:
    >
    > a= b() + c();
    >
    > The C++ standard does not specify whether the b() function should be called
    > before c(), or if c() should be called first, then b(), then the results be
    > added. A compiler is free to generate code that calls the two functions in
    > any order, before adding the results, and storing the result of the addition
    > in a. The C++ standard only defines "sequence points", where all evaluations
    > must be complete. In the above example, the semicolon is the sequence point.
    > This is somewhat of an oversimplification, but it gives you the general
    > idea.
    >
    > In your example:
    >
    > cout << "b = " << x << fun2( 2 ) << "ads" << endl;
    >
    > The function call to fun2() may be executed at any time before the code that
    > corresponds to " << "ads" ", as long as the return value from the function
    > call is sent to the stream in the right "spot".
    >
    > application_pgp-signature_part
    > < 1KViewDownload


    Thanks.Now I know "sequence points", and I saw a paragraph from other
    web site,
    which says that C++ group the "sequence points" into 5 situation:

    At the end of a full expression
    After the evaluation of all function arguments in a function call and
    before execution of any expressions in the function body
    After copying of a returned value and before execution of any
    expressions outside the function
    After evaluation of the first expression in a&&b, a||b, a?b:c, or
    a,b
    After the initialization of each base and member in the constructor
    initialization list

    Who has the Stardard C++ Doc about "sequence points" ?
    want.to.be.professer, Aug 29, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Taewoon Kwon

    Qustion about ithreads join

    Taewoon Kwon, Sep 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    421
    Joe Smith
    Sep 15, 2004
  2. John Tiger
    Replies:
    10
    Views:
    5,566
  3. ai@work
    Replies:
    9
    Views:
    529
    Ron Natalie
    Dec 16, 2004
  4. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    758
    red floyd
    Jan 15, 2005
  5. red floyd
    Replies:
    3
    Views:
    528
    Dietmar Kuehl
    Mar 8, 2005
Loading...

Share This Page