473,387 Members | 1,742 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,387 software developers and data experts.

Default parameter to function when passing by reference

Suppose I have the following function signature

void foo( Bar &myBar);

and I want myBar to be an optional parameter, is there a way to do the
logical equivalent of

void foo( Bar &myBar = myBar() )

when passing by reference? Every method I've tried gives me syntax
errors.

Thanks!

Feb 15 '07 #1
4 9166
On Feb 15, 12:23 pm, "Chris" <Thatch...@gmail.comwrote:
Suppose I have the following function signature

void foo( Bar &myBar);

and I want myBar to be an optional parameter, is there a way to do the
logical equivalent of

void foo( Bar &myBar = myBar() )

when passing by reference? Every method I've tried gives me syntax
errors.

Thanks!
how about
void foo( const Bar &myBar = myBar() )
?

Feb 15 '07 #2
On Feb 15, 10:30 am, smguo2...@gmail.com wrote:
On Feb 15, 12:23 pm, "Chris" <Thatch...@gmail.comwrote:
Suppose I have the following function signature
void foo( Bar &myBar);
and I want myBar to be an optional parameter, is there a way to do the
logical equivalent of
void foo( Bar &myBar = myBar() )
when passing by reference? Every method I've tried gives me syntax
errors.
Thanks!

how about
void foo( const Bar &myBar = myBar() )
?
I guess the example I gave there should really be

void foo( Bar &myBar = Bar() );

but anyway, I have every intention of modifying that variable, so
const (while it does fix the syntax problem) is not a viable
solution. Am I stuck making a copy of that variable either by passing
by copy or making a local copy in my function?

Feb 15 '07 #3
Chris wrote:
On Feb 15, 10:30 am, smguo2...@gmail.com wrote:
>On Feb 15, 12:23 pm, "Chris" <Thatch...@gmail.comwrote:
>>Suppose I have the following function signature
>>void foo( Bar &myBar);
>>and I want myBar to be an optional parameter, is there a way to do
the logical equivalent of
>>void foo( Bar &myBar = myBar() )
>>when passing by reference? Every method I've tried gives me syntax
errors.
>>Thanks!

how about
void foo( const Bar &myBar = myBar() )
?

I guess the example I gave there should really be

void foo( Bar &myBar = Bar() );

but anyway, I have every intention of modifying that variable,
That is a rather strange design decision. What is it you're intended
to modify if you pass a temporary in? And if it's documented (and thus
known) that 'foo' modifies its argument, what could be the point of
calling it without an argument?
so
const (while it does fix the syntax problem) is not a viable
solution. Am I stuck making a copy of that variable either by passing
by copy or making a local copy in my function?
No, you can have a global object you'd modify the hell out of when the
caller is careless enough not to supply the argument.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #4
On 15 Feb., 18:23, "Chris" <Thatch...@gmail.comwrote:
Suppose I have the following function signature

void foo( Bar &myBar);

and I want myBar to be an optional parameter, is there a way to do the
logical equivalent of

void foo( Bar &myBar = myBar() )

when passing by reference? Every method I've tried gives me syntax
errors.
That is because you can't do that. What is the problem with

void foo() { Bar myBar; foo(myBar);}

I actually recommend this method instead of having default parameters
in the general case. The advantage is the greater versatility if you
should ever want to pass foo as a parameter.

/Peter

Feb 15 '07 #5

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

Similar topics

26
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
6
by: Senthilvel | last post by:
Hi folks, My friend tells that the following function declaration is illegal. void Foo(const string& strData = "Default"); My friend argues that it is not legal to provide a default value for...
1
by: Mikko Penkkimäki | last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++. I know that arrays work like this: In C++ code: struct teststruct { char str; float...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
3
by: Curtis | last post by:
Hello everyone: Recently, I've come across the need to accept an arbitrary amount of arguments in my function, but I also need to alter the data in the calling scope. I have been trying to...
4
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, As the Subject suggests, array variable will retrogress as the parameter of the function actually taking pointer as its argument, like this: int f(int* i) { cout << sizeof(i) <<...
8
by: Yansky | last post by:
If I have the following function: function foo(){ alert('hi'); } and I don't need to pass any parameters to it, is calling it this way:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.