Automatic lvalue in functions

D

dogpuke

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?
 
T

TB

(e-mail address removed) 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?
 
Z

Zara

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top