Re: returning reference to pointer
On Jun 9, 1:17 pm, ajcpp...@gmail.com wrote:
#include <iostream>
#include <cstring>
char const* max (char const* a, char const* b)
{
return std::strcmp(a,b) < 0 ? b : a;
}
template <typename T>
T const& max (T const& a, T const& b, T const& c)
{
return max (max(a,b), c);
}
int main ()
{
const char* s1 = "foo";
const char* s2 = "bar";
const char* s3 = "betty";
std::cout << "::max(s1, s2, s3) = " << ::max(s1, s2, s3) <<
std::endl;
}
Are you sure that the 3 parameter max function is supposed to return a
reference at all? Your 2 parameter max function is returning a
temporary and you are returning a reference to that temporary in your
3 parameter version. Why not use code like this:
char const* imax (char const* a, char const* b)
{
return (std::strcmp(a,b) < 0) ? b : a;
}
template <typename T>
T const imax (T const& a, T const& b, T const& c)
{
return imax (imax(a,b), c);
}
Regards,
Ivan Novick
http://www.0x4849.net
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]