473,840 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2275
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
16249
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 integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
4
3272
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
5
2684
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
2506
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
2631
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
14819
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 expression: .... snip ...
2
1986
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 && (c=getchar())!=EOF && c!='\n'; ++i) s = c; if (c == '\n') {
6
5598
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 normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
30
3873
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 the end of the app for example: class test { public: int x;
0
9698
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,...
0
10597
jinu1996
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...
1
10656
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7834
isladogs
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...
0
7022
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();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4495
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
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.