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

Default Value while 'passing by reference'

We know C++ supports functions that can carry default values as
follows:

someFunction(int a, int b, int c =20);

So calling someFunction(x,y) is perfectly legal!

However, if I need to information by reference, can initiate the
referenced variable to a default value?

Eg:

int main()
{
int a = 20;
int b, c;

int &r = a;

someFunction(b,c); // <-------------------- POSSIBLE??
someFunction(b,c,r);//<-------------------- This is allowed

}

someFunction(int x, int y, int& z = 30); //<-- WELL??
{
}

Jul 23 '05 #1
3 4530

"mufasa" <so*****@gmail.com> wrote in message
We know C++ supports functions that can carry default values as
follows:

someFunction(int a, int b, int c =20);

So calling someFunction(x,y) is perfectly legal!

However, if I need to information by reference, can initiate the
referenced variable to a default value?

Eg:

int main()
{
int a = 20;
int b, c;

int &r = a;

someFunction(b,c); // <-------------------- POSSIBLE??
someFunction(b,c,r);//<-------------------- This is allowed

}

someFunction(int x, int y, int& z = 30); //<-- WELL??
There should be no semicolon at the end
{
}


This is not legal C++. You are binding a non const reference (z) to a
temporary object, which is illegal. It will work if you bind it to some
l-value.

int a = 30;

void
someFunction(int x, int y, int& z = a)
{
}

Sharad


Jul 23 '05 #2
mufasa wrote:
We know C++ supports functions that can carry default values as
follows:

someFunction(int a, int b, int c =20);

So calling someFunction(x,y) is perfectly legal!

However, if I need to information by reference, can initiate the
referenced variable to a default value?
Yes.
int main()
{
int a = 20;
int b, c;

int &r = a;

someFunction(b,c); // <-------------------- POSSIBLE??
someFunction(b,c,r);//<-------------------- This is allowed

}

someFunction(int x, int y, int& z = 30); //<-- WELL??
Your function is missing a return type.
{
}


Make that:

void someFunction(int x, int y, const int& z = 30); //<-- WELL??
{
}

Jul 23 '05 #3
mufasa wrote:
We know C++ supports functions that can carry default values as
follows: ....
someFunction(int x, int y, int& z = 30); //<-- WELL??
void someFunction(int x, int y, const int& z = 30) {
}


If you're passing a reference to a literal (30), does it make sense that
you can modify it ? The C++ standard says you can't.

G
Jul 23 '05 #4

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

Similar topics

36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
7
by: prefersgolfing | last post by:
In 6, by default arguments, were passed byref what is the default in .NET? Can you point me to any MSDN documentation? TIA
13
by: pauldepstein | last post by:
My texts give plenty of examples where passing by reference is necessary. For instance, the famous swap example. However, I've never seen an example where passing a variable by value is...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
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...

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.