473,320 Members | 2,020 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,320 software developers and data experts.

reference in function

hello ,

I want to create a new function that way:

void foo(int &x,int &y=0) {}

I mean that, I would use it both the way foo(x) and foo(x,y).

but the complier says that was invalid.

how can i make it .

thank u.

Jun 9 '06 #1
6 1515
jimmy wrote:
hello ,

I want to create a new function that way:

void foo(int &x,int &y=0) {}

A non-const reference requires an lvalue, 0 isn't one. This is because
your code might try and change the value referenced.

You have to use a const&.

--
Ian Collins.
Jun 9 '06 #2
In article <1149822787.892001.307610
@g10g2000cwb.googlegroups.com>, tb****@gmail.com says...
hello ,

I want to create a new function that way:

void foo(int &x,int &y=0) {}

I mean that, I would use it both the way foo(x) and foo(x,y).

but the complier says that was invalid.


A couple of possibilities. One is to simply overload the
function:

void foo(int &x, int &y) {
// whatever
}

void foo(int x) {
int y = 0;
foo(x, y);
}

Another is to create a variable and pass a reference to
it:

int default = 0;

void foo(int &x, int &y = default) {
// whatever
}

Just keep in mind that this probably doesn't make a whole
lot of sense. The primary reason to pass an int by
reference is so the function can modify the variable that
was passed -- and modifying 'default' won't accomplish
much (and may cause your program to do something strange
when/if default is changed to some non-zero value.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 9 '06 #3
jimmy wrote:
hello ,

I want to create a new function that way:

void foo(int &x,int &y=0) {}

I mean that, I would use it both the way foo(x) and foo(x,y).

but the complier says that was invalid.

how can i make it .


What about

void foo ( int & x, int & y ) {}

void foo ( int & x ) {
int dummy = 0;
foo( x, dummy );
}

Best

Kai-Uwe Bux

Jun 9 '06 #4

Kai-Uwe Bux wrote:
jimmy wrote:
hello ,

I want to create a new function that way:

void foo(int &x,int &y=0) {}

I mean that, I would use it both the way foo(x) and foo(x,y).

but the complier says that was invalid.

how can i make it .


What about

void foo ( int & x, int & y ) {}

void foo ( int & x ) {
int dummy = 0;
foo( x, dummy );
}

Best

Kai-Uwe Bux


Jun 9 '06 #5

void foo ( int & x, int & y ) {}

void foo ( int & x ) {
int dummy = 0;
foo( x, dummy );
}

Good.
Maybe this is the best solution.

Jun 9 '06 #6
void fooo(int &x,int &y = (int&)(*(int*)0))
{

}

jimmy 写道:
hello ,

I want to create a new function that way:

void foo(int &x,int &y=0) {}

I mean that, I would use it both the way foo(x) and foo(x,y).

but the complier says that was invalid.

how can i make it .

thank u.


Jun 13 '06 #7

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

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
3
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)':...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
3
by: George2 | last post by:
Hello everyone, 1. Returning non-const reference to function local object is not correct. But is it correct to return const reference to function local object? 2. If in (1), it is correct...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shllpp 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

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.