473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Passing "++a" to a function taking "int&"

Consider this code:

void foo(int& i)
{
i += 10;
}

int main()
{
int a = 1;
foo(++a);
foo(a++);
}

Using gcc 3.3.5 (with -Wall -W -pedantic -ansi) the first foo() call
is ok, but the second one causes an error.

I assume that this is indeed what the C++ standard says. However,
I would like to make sure that it indeed is so. (I'm more interested
in the "foo(++a)" working than the "foo(a++)").
Jun 7 '06 #1
3 2239
Juha Nieminen wrote:
Consider this code:

void foo(int& i)
{
i += 10;
}

int main()
{
int a = 1;
foo(++a);
foo(a++);
}

Using gcc 3.3.5 (with -Wall -W -pedantic -ansi) the first foo() call
is ok, but the second one causes an error.
That's because 'a++' does not yield an l-value, bun an r-value, IIRC,
and a non-const reference cannot be bound to an r-value (a temporary).
'++a' yields an l-value, the object itself, after it's incremented.
I assume that this is indeed what the C++ standard says. However,
I would like to make sure that it indeed is so.
Yes, it is indeed so.
(I'm more interested
in the "foo(++a)" working than the "foo(a++)").


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 7 '06 #2
Juha Nieminen wrote:
Consider this code:

void foo(int& i)
{
i += 10;
}

int main()
{
int a = 1;
foo(++a);
foo(a++);
}

Using gcc 3.3.5 (with -Wall -W -pedantic -ansi) the first foo() call
is ok, but the second one causes an error.

I assume that this is indeed what the C++ standard says. However,
I would like to make sure that it indeed is so. (I'm more interested
in the "foo(++a)" working than the "foo(a++)").


Yes, it is correct, the second passes a temporary (the result of a++) to
a function that takes a non-const reference. The first simply passes a.

--
Ian Collins.
Jun 7 '06 #3
a++: rerun a const object

const Type Type::operator++(int)
{
Type oldvalue = *this;
++(*this);
return oldvalue;
}

++a: return a no-const reference

Type& Type::operator++()
{
*this+=1;
return *this;
}

Jun 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
4
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
5
by: Nimmi Srivastav | last post by:
When I was learning C, I learned that the data type of a function name by itself is a function pointer. For example int someFunction(char* str) { .... }
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
2
by: arnuld | last post by:
on page 29, section 1.9 Character Arrays, i see an example: /* getline: read a line into s, return length */ int getline(char s,int lim) { int c, i; for (i=0; i < lim-1 &&...
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.