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

C++ reference arguments to functions

Hello All,

I have encountered a difference between compilers and need some help
understanding what is happening.

I have a function such as:

void MyClass::foo(GenericType& myarg){...}

when I call it such as:

main()
{
MyClass myObject;
..
..
..
myObject.foo( <C/C++ expression evaluating to a GenericType>);
}

It compiles and executes fine with one compiler (Sun Forte) but won't in
another
compiler (g++) unless I do the following:
(The compile error is that the argument needs to be an lvalue.....)

main()
{
MyClass myObject;
GenericType myTemp;
..
..
..
myTemp=<C/C++ expression evaluating to an object of type MyClass>
myObject.foo( myTemp);
}

Shouldn't the compiler track the results from the expression, along with

the appropriate lvalue nature of the return value, in the stack space
for that particular scope?

What exactly is this issue? A style/preference or a hint at some
programming error likely to cause some problem in the future?

Matt

Jul 19 '05 #1
2 3960
Matt Priest wrote:
Hello All,

I have encountered a difference between compilers and need some help
understanding what is happening.

I have a function such as:

/void MyClass::foo(GenericType& myarg){...}/

when I call it such as:

/main()/
/{/
/ MyClass myObject;/
/./
/./
/./
/ myObject.foo( <C/C++ expression evaluating to a GenericType>);/
/}/

It compiles and executes fine with one compiler (Sun Forte) but won't in
another
compiler (g++) unless I do the following:
(The compile error is that the argument needs to be an /lvalue./....)

/main()/
/{/
/ MyClass myObject;/
/ GenericType myTemp;/
/./
/./
/./
/ myTemp=<C/C++ expression evaluating to an object of type MyClass>/
/ myObject.foo( myTemp);/
/}/

Shouldn't the compiler track the results from the expression, along with
the appropriate /lvalue/ nature of the return value, in the stack space
for that particular scope?

What exactly is this issue? A style/preference or a hint at some
programming error likely to cause some problem in the future?

Matt


You need to declare foo() so as to take a reference to a *const*
GenericType, to persuade the compiler that foo() will not attempt to
modify the parameter:

void MyClass::foo(GenericType const & myarg);

--
Mike Smith

Jul 19 '05 #2
Matt Priest wrote:
Hello All,

I have encountered a difference between compilers and need some help
understanding what is happening.

I have a function such as:

void MyClass::foo(GenericType& myarg){...}

when I call it such as:

main()
This is invalid. You must supply a return type for all functions. In the
case of main, that return type must be int. There is no "implicit int"
rule in C++.

int main()
{
MyClass myObject;
.
.
.
myObject.foo( <C/C++ expression evaluating to a GenericType>);
}

It compiles and executes fine with one compiler (Sun Forte)


Then this is an error in the compiler. In C++ a temporary object (i.e.,
an object that results from the evaluation of an expression) cannot be
bound to a non-const reference.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

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

Similar topics

4
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in...
15
by: David Fisher | last post by:
I have been encouraged by someone else to use a reference rather than a pointer as an "out" parameter to a function, eg. void doStuff(Thing &out1, Thing &out2) as opposed to: void...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
6
by: Mark Brandyberry | last post by:
I have a bit of a problem with an overloaded operator that I'm writing that I have distilled down to an example in the code below. Essentially, I have an operator function (+=) that takes a...
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...
9
by: rakoo | last post by:
is there is any ways i pass default value to reference variable suct as fun(const int &i=8) as i can't do fun ( int &i=8) and modify variable i ,inside that funtion. rakesh Kushwaha
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
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...
7
by: Jess | last post by:
Hello, I learned that when I work with templates in C++, I should have functions that pass arguments by reference because the type of object is not known. Does it mean that if I have a function...
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....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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...

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.