Automatic lvalue in functions



 DEVELOP > c-Plus-Plus > Automatic lvalue in functions

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 21 Jan 2006 09:28:12 AM
Object: Automatic lvalue in functions
I have a number of functions that return structs. Some sample calls:
mystruct = func_one(mystruct, var1, var2);
mystruct = func_two(mystruct, whatever, scale, thirdvar);
In every call, I'm passing the lvalue mystruct. I think it makes the
code repetitive and cluttered to have the lvalue listed as a parameter
on every function call.
I would rather have:
mystruct = func_one(var1, var2);
mystruct = func_two(whatever, scale, thirdvar);
Yet still be able to access the lvalue within the function. Is there a
way using the preprocessor or some other trick to accomplish this? If
it were the preprocessor, it could parse this line:
mystruct = func_one(var1, var2);
Find the value to the left of the = and insert it with a comma after
func_one(
Is there a way to do this?
.

User: "TB"

Title: Re: Automatic lvalue in functions 21 Jan 2006 09:34:46 AM
sade:

I have a number of functions that return structs. Some sample calls:
mystruct = func_one(mystruct, var1, var2);
mystruct = func_two(mystruct, whatever, scale, thirdvar);

In every call, I'm passing the lvalue mystruct. I think it makes the
code repetitive and cluttered to have the lvalue listed as a parameter
on every function call.

I would rather have:
mystruct = func_one(var1, var2);
mystruct = func_two(whatever, scale, thirdvar);

Yet still be able to access the lvalue within the function. Is there a
way using the preprocessor or some other trick to accomplish this? If
it were the preprocessor, it could parse this line:
mystruct = func_one(var1, var2);

Find the value to the left of the = and insert it with a comma after
func_one(

Is there a way to do this?

What's stopping you from making them member functions?
--
TB @ SWEDEN
.

User: "Zara"

Title: Re: Automatic lvalue in functions 21 Jan 2006 10:32:28 AM
On 21 Jan 2006 07:28:12 -0800,
wrote:

I have a number of functions that return structs. Some sample calls:
mystruct = func_one(mystruct, var1, var2);
mystruct = func_two(mystruct, whatever, scale, thirdvar);

In every call, I'm passing the lvalue mystruct. I think it makes the
code repetitive and cluttered to have the lvalue listed as a parameter
on every function call.

I would rather have:
mystruct = func_one(var1, var2);
mystruct = func_two(whatever, scale, thirdvar);

Yet still be able to access the lvalue within the function. Is there a
way using the preprocessor or some other trick to accomplish this? If
it were the preprocessor, it could parse this line:
mystruct = func_one(var1, var2);

Find the value to the left of the = and insert it with a comma after
func_one(

Is there a way to do this?

Apart from making it a memeber function (as noted by TB), you may just
pass a non-const refrence to the struct:
void func_one (type_of_mystruct& mystruct, type1 var1, type2 var2);
You may the modify mystruct within func_one. But the syntax of calling
would be:
func_one(mystruct,var1,var2)
and not the one you desire.
*AND* it is much, much better, to make it a member function.
Zara
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER