[opensuse-programming] C++ overload question.
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); -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
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 -- "Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ... Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail --------------------------------------------------------------------- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-programming+help@opensuse.org
participants (2)
-
Jerry Feldman
-
Verdi March