I
Ioannis Vranos
Alf said:void client( bool thisOrThat )
{
std::string const gwb = "Bah!";
std::string const* ps = thisOrThat? &gwb : &iv.getstring();
std::cout << *ps << std::endl;
}
Making it more complete:
#include <iostream>
class SomeClass
{
std::string s;
public:
SomeClass():s("Test string") {}
const std::string &getstring() const { return s; }
};
void client( bool thisOrThat )
{
SomeClass iv;
std::string const gwb = "Bah!";
std::string const* ps = thisOrThat? &gwb : &iv.getstring();
std::cout << *ps << std::endl;
}
int main()
{
client(true);
}
Yes, if you convert const std::string & to const std::string you end up
getting the address of a temporary and all my compilers warn of this.
However this is an unusual use at first, to use a pointer to a returned
const string &, and the user as it seems can still take the address of
the temporary even if you return a temporary since the beginning.
If you want to make more obvious that this should not happen, you can
add to the documentation that the const references or const pointers
returned by these methods point to an implementation specific space that
can change after that call.
This is the case of all facilities of <ctime> BTW. All pointers returned
point to implementation-specific areas and can change after the call.