In porting some code I have a function (this is the original code). The second parameter has a default. Array is a home grown class. int foo(Array<aaa> &x, Array<bbb> *y = NULL);
What I want is to replace the home grown array classes with the STL vector class. I also want to pass them by reference. int foo(std::vector<aaa> &x, std::vector<bbb> &y); But, this requires 2 parameters. Using vector, I could overload foo with an inline single argument function that would achieve the desired result. inline int foo(std::vector<aaa> &x) { std::vector<bbb> y; // empty vector return foo(x, y); }
Is there a good way where I could use a single function call, using vector where the second parameter has a default value as the empty vector. This would also achieve the desired result, but with a performance hit if vector y is populated. int foo(std::vector<aaa> &x, std::vector<bbb> *y =NULL);
Hi,
Jerry Feldman wrote:
But, this requires 2 parameters. Using vector, I could overload foo with an inline single argument function that would achieve the desired result. inline int foo(std::vector<aaa> &x) { std::vector<bbb> y; // empty vector return foo(x, y); }
Is there a good way where I could use a single function call, using vector where the second parameter has a default value as the empty vector. This would also achieve the desired result, but with a performance hit if vector y is populated. int foo(std::vector<aaa> &x, std::vector<bbb> *y =NULL);
(My C++ is rusty). What about overloading foo: int foo(std::vector<aaa> &x) { std::vector<bbb> y; return foo(x, y); }
int foo(std::vector<aaa> &x, std::vector<bbb> &y) { return foo(x, y); }
In C++, only pointer can be assigned NULL. Also, in the one-argument version, y's scope is only in the function.
Regards, Verdi
programming@lists.opensuse.org