side effects?

S

Senthil

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
 
N

Nethali

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.
 
J

John Harrison

Senthil said:
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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
S

s_amrollahi

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
 
J

Jim Langston

Senthil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top