side effects?

Discussion in 'C++' started by Senthil, Jun 27, 2007.

  1. Senthil

    Senthil Guest

    Hi,
    Whenever i read a C++ book or a newsgroup posting, i come across the
    terms like " before the side effects completes" , "destructor with
    side effects" etc. What is this side effect mean in C++ world?
    Is it like something that is not desired but happens automatically and
    we cannot prevent it?

    Would be great if someone throws light on this.

    Thanks,
    Senthil
    Senthil, Jun 27, 2007
    #1
    1. Advertising

  2. Senthil

    Nethali Guest

    On Jun 27, 2:14 pm, Senthil <> wrote:
    > Hi,
    > Whenever i read a C++ book or a newsgroup posting, i come across the
    > terms like " before the side effects completes" , "destructor with
    > side effects" etc. What is this side effect mean in C++ world?
    > Is it like something that is not desired but happens automatically and
    > we cannot prevent it?
    >
    > Would be great if someone throws light on this.
    >
    > Thanks,
    > Senthil


    All operators produce a value from their operands. This value is
    produced without modifying the operands, except with the assignment,
    increment, and decrement operators. Modifying an operand is called a
    side effect. The most common use for operators that modify their
    operands is to generate the side effect, but you should keep in mind
    that the value produced is available for your use just as in operators
    without side effects.
    Nethali, Jun 27, 2007
    #2
    1. Advertising

  3. Senthil wrote:
    > Hi,
    > Whenever i read a C++ book or a newsgroup posting, i come across the
    > terms like " before the side effects completes" , "destructor with
    > side effects" etc. What is this side effect mean in C++ world?
    > Is it like something that is not desired but happens automatically and
    > we cannot prevent it?
    >
    > Would be great if someone throws light on this.
    >
    > Thanks,
    > Senthil
    >


    It's not a C++ term, it applies to more or less any programming language.

    The first paragraph of this
    http://en.wikipedia.org/wiki/Side_effect_(computer_science) is a
    reasonable definiton.

    john
    John Harrison, Jun 27, 2007
    #3
  4. John Harrison wrote:
    > Senthil wrote:
    >> Hi,
    >> Whenever i read a C++ book or a newsgroup posting, i come across the
    >> terms like " before the side effects completes" , "destructor with
    >> side effects" etc. What is this side effect mean in C++ world?
    >> Is it like something that is not desired but happens automatically and
    >> we cannot prevent it?
    >>
    >> Would be great if someone throws light on this.
    >>
    >> Thanks,
    >> Senthil
    >>

    >
    > It's not a C++ term, it applies to more or less any programming language.
    >
    > The first paragraph of this
    > http://en.wikipedia.org/wiki/Side_effect_(computer_science) is a
    > reasonable definiton.
    >
    > john


    In C++ I guess you would exclude from the definition methods which
    modify the state of the 'this' object, since this is an anticipated
    feature of OO programming.

    john
    John Harrison, Jun 27, 2007
    #4
  5. On 2007-06-27 07:46, Nethali wrote:
    > On Jun 27, 2:14 pm, Senthil <> wrote:
    >> Hi,
    >> Whenever i read a C++ book or a newsgroup posting, i come across the
    >> terms like " before the side effects completes" , "destructor with
    >> side effects" etc. What is this side effect mean in C++ world?
    >> Is it like something that is not desired but happens automatically and
    >> we cannot prevent it?
    >>
    >> Would be great if someone throws light on this.
    >>
    >> Thanks,
    >> Senthil

    >
    > All operators produce a value from their operands. This value is
    > produced without modifying the operands, except with the assignment,
    > increment, and decrement operators. Modifying an operand is called a
    > side effect. The most common use for operators that modify their
    > operands is to generate the side effect, but you should keep in mind
    > that the value produced is available for your use just as in operators
    > without side effects.


    It should be noted though that side effects can be much more than just
    modifications of the operands, it can also involve modifications of
    other objects, global data, I/O operations etc. Basically, anything that
    makes a changes beside the return-value is a side effect.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 27, 2007
    #5
  6. Senthil

    Guest

    On Jun 27, 6:14 am, Senthil <> wrote:
    > Hi,
    > Whenever i read a C++ book or a newsgroup posting, i come across the
    > terms like " before the side effects completes" , "destructor with
    > side effects" etc. What is this side effect mean in C++ world?
    > Is it like something that is not desired but happens automatically and
    > we cannot prevent it?
    >
    > Would be great if someone throws light on this.
    >
    > Thanks,
    > Senthil


    Hi Senthil,
    side effect is a term used in programming language in general. For
    example see the classic book
    Programming Languages by Terence W. Pratt.
    In C++ operators usually have side effects, Of course side effect is
    not bad things actually sometimes they are needed. For example the
    statement
    cout << "Hello, world!";
    has one operator (<<) and two operands: cout and "Hello, world!". It
    means put to "Hello, world" string into cout object. As its side
    effect the greeting message will be showed on screen. Indeed in a
    typical hello, world program, the side effect do the main task of the
    program.

    Regards,
    S. Amrollahi
    , Jun 27, 2007
    #6
  7. Senthil

    Jim Langston Guest

    "Senthil" <> wrote in message
    news:...
    > Hi,
    > Whenever i read a C++ book or a newsgroup posting, i come across the
    > terms like " before the side effects completes" , "destructor with
    > side effects" etc. What is this side effect mean in C++ world?
    > Is it like something that is not desired but happens automatically and
    > we cannot prevent it?
    >
    > Would be great if someone throws light on this.


    Consider a simple side effect, someone writes a class and wants to keep
    track/debug constructors, destructors and such and so in each does std::cout
    << "Class X Constructor", std::cout << "Class X Destructor", std::cout <<
    "Class X Copy Constructor", etc...

    Those are side effects (the outputs). Compilers may be free to not do a
    copy constructor when one would normally be done if it can do it more
    effeciently some other way. For example, say there is an instruciton that
    would normally create a temporary object using a constructor then use a copy
    constructor to create an object (or assignment operator). The compiler may
    just deside to create the object directory from the constructor doing away
    with the copy or assignments. Normally, that's fine, but in this case we
    don't see the side effects, the output of "Class X Copy Constructor", etc...

    It is a little more detailed than that, that is a simplistic description
    because I really don't know all what exactly makes up a side effect. I do
    know that it is often refered to in if statments:

    if ( A || B )

    B may not always be evaluated (if A is true then the if statment is
    short-circuited, only A is evaluated) and so if B has side effects you may
    expect to see them but may not. Consider a simplistic case: (untested code)

    class Foo
    {
    public:
    Foo( bool Enable ): Enabled_( Enable ) {}
    bool Enabled() { std::cout << "Checking if enabled...\n'; return
    Enabled_; }
    private:
    bool Enabled_;
    }

    int main()
    {
    bool Bar = true;
    Foo Foobar( false );

    if ( Bar || Foo.Enabled() }
    std::cout << "One is true...\n";
    }

    At first glance, one may expect the output of this program to be:
    Checking if enabled...
    One is true...

    But the output will most likely be:
    One is true...

    Because of short circuiting, Foo.Enabled() is not evaluated and it's "side
    effects" are not performed.
    Jim Langston, Jun 28, 2007
    #7
    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. Jim Bancroft
    Replies:
    2
    Views:
    2,387
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=
    Dec 28, 2004
  2. Anonieko
    Replies:
    0
    Views:
    1,940
    Anonieko
    Aug 12, 2005
  3. VisionSet
    Replies:
    7
    Views:
    471
    Michael Rauscher
    Nov 9, 2004
  4. Jason Heyes
    Replies:
    10
    Views:
    537
    red floyd
    Dec 21, 2004
  5. Giuseppe

    Re: Newbie-Side effects?

    Giuseppe, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,157
    Giuseppe
    Jun 24, 2003
Loading...

Share This Page