Dear all,
sorry, I always forget this thing:
Is this foo:
int foo(int i)
{
++i;
return i-1;
}
the same like this foo:
int foo(int i)
{
return i++;
}
? As I am alway scared using the ++ operators in compact forms, I'd better
ask......
Thanks,
Patrick
*************** *************
Reply form:
[ ] both foos are identical in the functionality and well defined
[ ] keep your fingers away from "return i++"
[ ] keep your fingers away from "return ++i"
[ ] do not know
Please mark the correct answers. More than one answer might be correct. 23 5769
Patrick Kowalzick wrote: sorry, I always forget this thing:
Is this foo:
int foo(int i) { ++i; return i-1; }
the same like this foo:
int foo(int i) { return i++; }
Yes. And it really is the same as
int foo(int i)
{
return i;
} ? As I am alway scared using the ++ operators in compact forms, I'd better ask......
Thanks, Patrick
*************** *************
Reply form:
[ ] both foos are identical in the functionality and well defined [ ] keep your fingers away from "return i++" [ ] keep your fingers away from "return ++i" [ ] do not know
Please mark the correct answers. More than one answer might be correct.
You better mark the first one and the last one. Both answers are correct.
V
Dear Victor, You better mark the first one and the last one. Both answers are correct.
[x] both foos are identical in the functionality and well defined
[ ] keep your fingers away from "return i++"
[ ] keep your fingers away from "return ++i"
[x] did not know
Thanks ;),
Patrick
int Blah()
{
int k = 5;
return ++k;
}
The above function returns 6.
--
The below function returns 5.
int Blah()
{
int k = 5;
return k++;
}
--
When you put ++ before the name: The object is incremented. The value of the
expression is the object's value *after* the increment.
When you putt ++ after the name: The value of the expression is the object's
value *before* the increment. The object is incremented.
Maybe the following will enlighten?:
class Number
{
private:
unsigned k;
public:
Number& operator++()
{
//This gets called when you do ++object
//Note that it returns by reference
k += 1;
return *this;
}
Number operator++(int)
{
//This gets called when you do object++
//Note that it returns by value
//First we create a temporary, making a copy of the current object
Number temp = *this;
//Now we proceed with the increment
k += 1;
//But... we return the old value, ie. before the increment
return temp;
}
};
-JKop When you put ++ before the name: The object is incremented. The value of the expression is the object's value *after* the increment.
When you putt ++ after the name: The value of the expression is the object's value *before* the increment. The object is incremented.
Here's the simple way to remember it: Just read from left to right as
normal:
++object;
[increment], then [object];
object++;
[object], then [increment];
Hopefully you're not Arabic!
-JKop
Dear JKop,
thanks a lot for your explanations. Sorry that I did not express me right,
but I know the thing about post- and pre-increment.
The only problem I have, is to remember in which cases I run into undefined
behaviour. So I'd better ask before ;). The compilers are not a good
indicator here, because some may work, and others not (undefined).
I was only *quite* sure that my code is fine, but not 100%.
Thanks,
Patrick
Patrick Kowalzick wrote: Dear JKop,
thanks a lot for your explanations. Sorry that I did not express me right, but I know the thing about post- and pre-increment.
The only problem I have, is to remember in which cases I run into undefined behaviour. So I'd better ask before ;).
In a nutshell:
* make sure the variable that gets incremented is not the same variable
assigned to.
i = i++; // <- That's a no, no
* don't increment the same variable more then once in a statement
j = i++ + i++; // no, no
foo( i++, i++ ); // no, no
cout << i++ << i++; // no, no
If you don't do any of the above you should be fairly safe
(Did I miss some other common mistakes?)
--
Karl Heinz Buchegger kb******@gascad .at
JKop wrote: When you put ++ before the name: The object is incremented. The value of the expression is the object's value *after* the increment.
When you putt ++ after the name: The value of the expression is the object's value *before* the increment. The object is incremented.
Actually, in both cases:
1. The object is incremented.
2. The value returned is either the original or
original value plus one.
You can't make any statement about WHEN the increment occurs.
Actually, I wouldn't have writen either ++k or k++.
Since I don't really care to change k, just get a value
one greater than it:
return k+1;
is MORE appropriate in my opinion.
Karl Heinz Buchegger wrote: Patrick Kowalzick wrote:
Dear JKop,
thanks a lot for your explanations. Sorry that I did not express me right, but I know the thing about post- and pre-increment.
The only problem I have, is to remember in which cases I run into undefined behaviour. So I'd better ask before ;).
In a nutshell: * make sure the variable that gets incremented is not the same variable assigned to.
i = i++; // <- That's a no, no
* don't increment the same variable more then once in a statement
j = i++ + i++; // no, no foo( i++, i++ ); // no, no cout << i++ << i++; // no, no
If you don't do any of the above you should be fairly safe (Did I miss some other common mistakes?)
Actually, variables is a bit too loose a requirement. It has to be
'object'. It is possible to increment the same object using different
variables:
int i = 42;
int& j = i;
i = j++; // same object, different variables
or even functions:
extern int i;
int foo() {
return i++;
}
int bar() {
i = foo(); // extremely obscured -- same object, and you don't
// even see the increment
}
V
Karl Heinz Buchegger wrote: ... In a nutshell: * make sure the variable that gets incremented is not the same variable assigned to.
i = i++; // <- That's a no, no
* don't increment the same variable more then once in a statement
j = i++ + i++; // no, no foo( i++, i++ ); // no, no cout << i++ << i++; // no, no
If you don't do any of the above you should be fairly safe (Did I miss some other common mistakes?) ...
You covered only the first part of the rule - multiple modifications
lead to UB. There's a second part as well - reading the old value for a
purpose other that calculating the new value leads to UB. For example
int i, j;
...
j = i++ + i; // no, no
Of course, it would also be useful to explain the difference between
built-in and user-defined operators (and when undefined behavior turns
into unspecified behavior) but I'm afraid we'll need a rather large
nutshell for all this.
--
Best regards,
Andrey Tarasevich This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Phil Powell |
last post by:
My first time working with a PHP class, and after 6 hours of working out the
kinks I am unable to return a value from the class, so now I appeal to the
general audience what on earth did I do wrong this time?
This is the code the retrieves the values:
if (($hasRegistered || $hasPreRegistered) && !empty($uplinenumber)) {
// CHECK TO SEE IF UPLINE NUMBER IS A VALID NUMBER
$regNumberGenerator = new RegNumberGenerator($uplinenumber,...
|
by: Jakob Bieling |
last post by:
Hi!
I am using VC++ 7.1 and have a question about return value optimization.
Consider the following code:
#include <list>
#include <string>
struct test
{
|
by: cppaddict |
last post by:
I'd like to know what goes on under the hood when methods return
objects. Eg, I have a simple Point class with two members _x and _y.
It's constructor, copy constructor, assignment operator and additon
operator (which returns another Point object, and which my question is
about) are as follows:
Point::Point(int x, int y) :
_x(x),
_y(y) { }
|
by: PengYu.UT |
last post by:
I have the following sample program, which can convert function object
with 1 argument into function object with 2 arguments. It can also do +
between function object of the same type.
The last line is very long. I'm wondering if there is any way to
suppress it. I can only think of typedef. But I'm not sure whether I
can use typedef for the return type.
Would you please help me? Please don't be daunted by the length of the
code.
|
by: Rhino |
last post by:
I am trying to verify that I correctly understand something I saw in the DB2
Information Center.
I am running DB2 Personal Edition V8.2.1 on Windows. I came across the
following in the Info Center:
To return a result set from a procedure to the originating application, use
the WITH RETURN TO CLIENT clause. When WITH RETURN TO CLIENT is specified on
a result set, no nested procedures can access the result set.
| |
by: Greenhorn |
last post by:
Hi,
when a function doesn't specify a return type ,value what value is
returned. In the below programme, the function sample()is returning the
value passed to 'k'.
sample(int);
main()
{
int i = 0,j;
j = sample(0);
|
by: Mark Jerde |
last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#.
This morning I thought I was getting somewhere, successfully getting back
the correct answers to a C++ " int SumArray(int ray, int count)"
Now I'm having problems with C++ "return(false)" being True in C#.
Here is the C# code.
=========================
using System;
using System.Runtime.InteropServices; //
|
by: Michael Maes |
last post by:
Hello,
I have a BaseClass and many Classes which all inherit (directly) from the BaseClass.
One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk.
1. The Deserialization goes like:
#Region " Load "
|
by: kikazaru |
last post by:
Is it possible to return covariant types for virtual methods inherited
from a base class using virtual inheritance?
I've constructed an example below, which has the following structure:
Shape = base class
Triangle, Square = classes derived from Shape
Prism = class derived from Shape
TriangularPrism, SquarePrism = classes derived from Triangle and
Prism, or Square and Prism respectively
|
by: KoreyAusTex |
last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()?
import java.util.*;
public class Card
{
// instance variables
//suits
private int suit;
private int spades;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |