1

I am trying to write a function that takes in a variable number of parameters. My research has directed me to learn about va_list and its methods (va_start, va_end, va_arg). The issue is that the parameters that I am passing into this function are references to objects created outside the function.

myClass obj1, obj2, obj3;
modifyObjects(3, obj1, 55, obj2, 33, obj, 35)

Here is the implementation I tried:

void modifyObjects(int numObjects, ...)
{
  va_list;
  va_start(list, numObjects);
  int i;
  for(i=0;i<numObjects;++i)
  {
    myClass* tempObjectHandle = va_arg(list, &myClass ); //get the reference to the object (THIS DOES NOT WORK!)
    int size = va_arg(list, int); //get the size
    tempObjectHandle->set(size); //tempObjectHandle should be pointing to object defined outside the function to set its size
  }
  va_end(list);
}

Is there any way to create objects and pass a variable list of references to those objects into a function so that once the function returns, all objects have been modified?

ADDITIONAL INFO:

I am also limited by to the C++03 standard that that is what is supported by the ARM compiler.

Thanks!

radensb
  • 664
  • 1
  • 7
  • 25
  • Any particular reason you can't use an std::vector& ? – manasij7479 Jul 29 '13 at 19:59
  • 1
    Does your compiler have C++11 support? What is the concrete problem you are trying to solve, not the problem you ran into after you picked your solution? (the problem isn't the function call: the problem is what the function call is supposed to solve) – Yakk - Adam Nevraumont Jul 29 '13 at 20:01
  • @manasij7479: A reference to a vector is not the same as a vector of references (which is impossible). – Beta Jul 29 '13 at 20:03
  • 1
    You almost never want to write a variadic function in C++. You could pass some sort of container-like object holding the values you care about, or you could create a variadic function template (quite different from a variadic function). – Jerry Coffin Jul 29 '13 at 20:05
  • Fixing suggest linked from earlier [Variable number of arguments in C++?](http://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c%5D) I think all the possible alternatives are mentioned in this thread. – Shafik Yaghmour Jul 29 '13 at 20:09
  • @Beta: Why won't a reference to a vector work? If the objects are of same type, and are coming to the same function to be modified, chances are, they are being (or can be) stored in some container...sending a reference to that is much cleaner that writing a lot to template code, if not required. – manasij7479 Jul 29 '13 at 20:10
  • I am trying to limit heap usage as much as possible so I am staying away from vectors. The goal was to simply have a function that can take a variable number of objects defined outside the function can modify them. The below answer works if I pass the address of the object to the function (&obj1), like I illustrated in the OP. I am wondering if it is also possible to get a reference in the function. I have edited the OP to illustrate what I meant. – radensb Jul 29 '13 at 21:10
  • @manasij7479: If the objects are in a `vector`, it will work; if they aren't, it won't. (And trying to copy them into a `vector` beforehand and copy them out again afterward -- as you seem to suggest with *"or can be"* -- could be quite tedious.) – Beta Jul 29 '13 at 22:08

1 Answers1

1
myClass* tempObjectHandle = va_arg(list, &myClass );

This should be myClass* tempObjectHandle = va_arg(list, myClass* );

You keep saying that you are passing references to modifyObjects. In fact, you are passing pointers.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • My mistake. I meant to write: 'modifyObjects(3, obj1, 55, obj2, 33, obj, 35);' as the function call. This works though! – radensb Jul 29 '13 at 21:05
  • 1
    You can't pass parameters to a variadic function by reference - only by value (whereby a copy is made) or by pointer. Passing by value would defeat your purpose though: `modifyObjects` won't be able to actually modify objects in a way that the caller can observe. – Igor Tandetnik Jul 30 '13 at 00:25
  • Understood. I got it working as I needed with your fix above and now I know the correct way to do this in C++03. Awesome! Thanks again! – radensb Jul 30 '13 at 16:36