C++ reference arguments to functions

M

Matt Priest

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
 
M

Mike Smith

Matt said:
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);
 
K

Kevin Goodsell

Matt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top