Automatic lvalue in functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dogpuke@gmail.com

    #1

    Automatic lvalue in functions

    I have a number of functions that return structs. Some sample calls:
    mystruct = func_one(mystru ct, var1, var2);
    mystruct = func_two(mystru ct, 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(whatev er, 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?

  • TB

    #2
    Re: Automatic lvalue in functions

    dogpuke@gmail.c om sade:[color=blue]
    > I have a number of functions that return structs. Some sample calls:
    > mystruct = func_one(mystru ct, var1, var2);
    > mystruct = func_two(mystru ct, 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(whatev er, 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?
    >[/color]

    What's stopping you from making them member functions?

    --
    TB @ SWEDEN

    Comment

    • Zara

      #3
      Re: Automatic lvalue in functions

      On 21 Jan 2006 07:28:12 -0800, dogpuke@gmail.c om wrote:
      [color=blue]
      >I have a number of functions that return structs. Some sample calls:
      >mystruct = func_one(mystru ct, var1, var2);
      >mystruct = func_two(mystru ct, 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(whatev er, 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?[/color]


      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_mystru ct& mystruct, type1 var1, type2 var2);

      You may the modify mystruct within func_one. But the syntax of calling
      would be:

      func_one(mystru ct,var1,var2)

      and not the one you desire.

      *AND* it is much, much better, to make it a member function.

      Zara

      Comment

      Working...