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

Default reference variables

I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());

Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.

Thanks,
James

Nov 10 '06 #1
16 2352
jryden wrote:
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());
No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.
>
but not this:

void func2(int &i = int());

Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.
Do you have to have it non-const? What for?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 10 '06 #2

jryden wrote:
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());
The parameter reference needs to be declared const in order to pass a
temporary:

void func2(const int& = int())

The assumption is that the program is calling func2() in order to
obtain the value it returns through the non-const parameter. So if the
program just throws that value away, why is it calling func2() in the
first place? Or, why isn't func2()'s parameter const?

Greg

Nov 10 '06 #3
jryden wrote:
[snip]
Can someone explain why? I would like a default reference parameter
using a simple type.
Urm. Why do you want to default an argument that is a reference?
I'd expect that if you called a function without an argument, and it
wanted a reference, that this would be an error. I'm presuming that
the reason to pass a reference is so that the function can change
the value of the calling code's variable. If you only want the value
to be passed down, and not access to it, at least for a simple type,
why pass a reference?

Also, what's wrong with overloading with a version of the func that
has no arguments?
Socks

Nov 10 '06 #4

Victor Bazarov wrote:
jryden wrote:
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.
Perhaps I should rephrase this to "my compiler allows me to do this".
In any case it does what I intend it to do...(Microsoft C++?)
>

but not this:

void func2(int &i = int());

Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.

Do you have to have it non-const? What for?
My intention is to have a return variable, therefore, non-const (an
[out] parameter) but I'd like the parameter to have a default value.
This is so that the hundreds of other calls to this function in my code
base don't need to be modified to pass this value as a parameter if
they don't care about what it returns.

This is typically done by doing the following:
void func3(int *i = NULL)
{
if (i = NULL) *i = x;
}

This is a constuction is don't like especially if there are many places
in the function where we would like to set the value of i.

Is there a better way to accomplish the this?
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 10 '06 #5
* jryden:
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());
As Victor already wrote, no you can't.

Adding to that, some compilers allow you to do that as a non-standard
extension.

If your compiler is one such, then it's a good idea to turn off that
language extension, if possible.

but not this:

void func2(int &i = int());

Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)
Right.

Can someone explain why?
Stroustrup's original explanation for why an alias for (reference to) a
non-const can't be bound to an rvalue, is that that could easily lead to
surprising results like apparently modifying the value of 42 -- while
in reality of course just modifying the temporary. I seem to recall
that Pete Becker once posted a much more forceful argument in this
group, involving function arguments and overloading. Something like
calling with an int argument for a formal unsigned& -- I can't recall
-- but now that I've mentioned it, you would not want this

void doTheThing( unsigned& x ) { x = 666; }

int main()
{
int nastyValue;
doTheThing( nastyValue );
std::cout << nastyValue << std::endl;
}

to be accepted, much less produce garbage output.

I would like a default [presumably non-const] reference parameter using
a simple type.
You can do

void func2( int& i ) { ... }
void func2() { int i = 0; func2( i ); }

or you can do

int defaultI = 0;

void func2( int& i = ::defaultI ) { ... }

or whatever.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 10 '06 #6

"jryden" <jr****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
>
My intention is to have a return variable, therefore, non-const (an
[out] parameter) but I'd like the parameter to have a default value.
This is so that the hundreds of other calls to this function in my code
base don't need to be modified to pass this value as a parameter if
they don't care about what it returns.

This is typically done by doing the following:
void func3(int *i = NULL)
{
if (i = NULL) *i = x;
that should be
if (i == NULL)

Some people prefer
if (NULL == i)

That way, if you use = accidently, the compiler will complain, since you
can't assign to NULL.
}

This is a constuction is don't like especially if there are many places
in the function where we would like to set the value of i.

Is there a better way to accomplish the this?
I've done this some times:

void f( int* pInt )
{
int temp;
if (!pInt)
pInt = &temp;
...
*pInt = whatever;
...
*pInt = whateverelse;
...
}

That lets me do the NULL check just once at the top of the function, and
keep the code below clean.

-Howard

Nov 10 '06 #7


On Nov 10, 10:58 am, "Howard" <alic...@hotmail.comwrote:
"jryden" <jry...@gmail.comwrote in messagenews:11**********************@b28g2000cwb.g ooglegroups.com...
My intention is to have a return variable, therefore, non-const (an
[out] parameter) but I'd like the parameter to have a default value.
This is so that the hundreds of other calls to this function in my code
base don't need to be modified to pass this value as a parameter if
they don't care about what it returns.
This is typically done by doing the following:
void func3(int *i = NULL)
{
if (i = NULL) *i = x;that should be
if (i == NULL)

Some people prefer
if (NULL == i)

That way, if you use = accidently, the compiler will complain, since you
can't assign to NULL.
}
This is a constuction is don't like especially if there are many places
in the function where we would like to set the value of i.
Is there a better way to accomplish the this?I've done this some times:

void f( int* pInt )
{
int temp;
if (!pInt)
pInt = &temp;
...
*pInt = whatever;
...
*pInt = whateverelse;
...

}That lets me do the NULL check just once at the top of the function, and
keep the code below clean.

-Howard
Okay, I think I have a couple of more standard solutions now. Thanks
for the info.

Nov 10 '06 #8
On Fri, 10 Nov 2006 10:37:36 -0500 in comp.lang.c++, "Victor Bazarov"
<v.********@comAcast.netwrote,
>void func1(myInt &i = myInt());

No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.
It's OK if myInt() returns a reference.

Nov 10 '06 #9
David Harmon wrote:
>>void func1(myInt &i = myInt());

No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.

It's OK if myInt() returns a reference.
:) Great idea! I wonder, however, if it is possible to come up with a correct
set of declarations in order to turn the OP's declaration into a valid one. This
is close

struct myInt {};
myInt& myInt();

void foo(struct myInt& i = myInt())
{
}

but if I remove the 'struct' from the parameter declaration Comeau will complain
with

"ComeauTest.c", line 5: error: incomplete type is not allowed
void foo(myInt& i = myInt())
^
"ComeauTest.c", line 5: error: identifier "i" is undefined
void foo(myInt& i = myInt())
^

--
Best regards,
Andrey Tarasevich
Nov 10 '06 #10
jryden wrote:
Victor Bazarov wrote:
>jryden wrote:
>>I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

No, you can't. A reference to non-const object (sometimes
referred to as "a const reference") cannot be bound to a temporary.

Perhaps I should rephrase this to "my compiler allows me to do
this". In any case it does what I intend it to do...(Microsoft C++?)
That's because you use a compiler specific language extension. If you try it
with a /Za compiler option, it will not compile.
Bo Persson
Nov 10 '06 #11
On Fri, 10 Nov 2006 11:09:20 -0800 in comp.lang.c++, Andrey Tarasevich
<an**************@hotmail.comwrote,
>David Harmon wrote:
>>>void func1(myInt &i = myInt());

No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.

It's OK if myInt() returns a reference.

:) Great idea! I wonder, however, if it is possible to come up with a correct
set of declarations in order to turn the OP's declaration into a valid one.
Ouch; I failed to notice that myInt is also the type.
I was thinking more in the line of a singleton of type int.
myInt ... rather bad type name I think.

Nov 10 '06 #12


On Nov 10, 3:31 pm, "Bo Persson" <b...@gmb.dkwrote:
jryden wrote:
Victor Bazarov wrote:
jryden wrote:
I can do this:
>struct myInt
{
int i;
};
void func1(myInt &i = myInt());
No, you can't. A reference to non-const object (sometimes
referred to as "a const reference") cannot be bound to a temporary.
Perhaps I should rephrase this to "my compiler allows me to do
this". In any case it does what I intend it to do...(Microsoft C++?)That's because you use a compiler specific language extension. If you try it
with a /Za compiler option, it will not compile.
....Neither will many other windows specific core files (specifically
ATL). Interestingly microsoft documentation explains that /Ze,
although deprecated, is also the default setting. I'll have to leave
it on for now but if and when we decide to port our code to another
platform we might have some interesting problems...but this is off
topic now.
>
Bo Persson- Hide quoted text -- Show quoted text -
Nov 10 '06 #13


On Nov 10, 5:53 pm, David Harmon <sou...@netcom.comwrote:
On Fri, 10 Nov 2006 11:09:20 -0800 in comp.lang.c++, Andrey Tarasevich
<andreytarasev...@hotmail.comwrote,
David Harmon wrote:
>>void func1(myInt &i = myInt());
>>No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.
It's OK if myInt() returns a reference.
:) Great idea! I wonder, however, if it is possible to come up with a correct
set of declarations in order to turn the OP's declaration into a valid one.Ouch;
I failed to notice that myInt is also the type.
I was thinking more in the line of a singleton of type int.
myInt ... rather bad type name I think.
I have lost the train of this thread, I think. myInt was just a quick
example struct from the original post (perhaps it should be MyInt)
anyway, I have been conviced that function overloading is better way to
accomplish my goal and I have changed my code accordingly.

Nov 10 '06 #14
David Harmon wrote:
>>>>void func1(myInt &i = myInt());

No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.

It's OK if myInt() returns a reference.

:) Great idea! I wonder, however, if it is possible to come up with a correct
set of declarations in order to turn the OP's declaration into a valid one.

Ouch; I failed to notice that myInt is also the type.
I was thinking more in the line of a singleton of type int.
myInt ... rather bad type name I think.
Yet it can be done :) I just had an idea

struct myInt {};
myInt& bar();
#define myInt() bar()

void foo(myInt& i = myInt())
{
}

--
Best regards,
Andrey Tarasevich
Nov 10 '06 #15

"jryden дµÀ£º
"
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());
I think a reference is just a symbol.This symbol is just occur in our
code.When call such function that take references as arguments, they
will be replaced with the actual addresses of parameters which you
reference to.When you dasm your program you will find out how it
work.You can just using this
voi func2(int &i = *(new int))
Thus,it enable i to reference to a address that store a int.

Nov 12 '06 #16

"programmer дµÀ£º
"
"jryden дµÀ£º
"
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());
I think a reference is just a symbol.This symbol is just occur in our
code.When call such function that take references as arguments, they
will be replaced with the actual addresses of parameters which you
reference to.When you dasm your program you will find out how it
work.You can just using this
voi func2(int &i = *(new int))
Thus,it enable i to reference to a address that store a int.
I'm sorry.I have made a big problem.Never try to use this.It has a
problem with how you release your memory allocated by new.It just tell
you how reference works.

Nov 12 '06 #17

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 =...
10
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
29
by: John Wood | last post by:
Even though CSC does its best to detect use of unassigned variables, it often misses it... for example if you just declare a double in a class without assigning a default value, it has a default...
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: Justin | last post by:
Is there any way to prevent VB.net to give default value to a variable (changing settings what not...)? For example Dim test As Integer test = test + 1 the statement "test = test + 1"...
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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,...

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.