* jimjim:
I ve come across the following code fragment and I was wondering why is the
copy ctr called on return (rather than just returning the string statement
obj.
string PublishedProductsRepo :: CreateStatement() const {
string statement;
statement ="SELECT DISTINCT "
"* "
"FROM "
"map";
return string(statement); }
Whether the copy constructor is called depends on your compiler and the
context of the CreateStatement call.
There is much that is unnecessary in the code, but it doesn't affect
copy constructor calls. What's important re copy constructor calls is
your compiler's optimizations.
A straightforward coding of this function is
std::string PublishedProductsRepo::CreateStatement() const
{
return "SELECT DISTINCT * FROM map";
}
Note the qualification with "std::".
If this inline function definition is in a header file, which is likely,
the lack of such qualification in the original code indicates there is a
"using namespace std;" or "using std::string;" in the header file, which
in turn indicates an incompetent programmer. In other words, if those
reasonable assumptions hold, then this is not code to learn from.
Rather, it's then code from which you can learn how to /not/ do things.
For example:
* Don't ever place "using namespace std;" in a header file.
* Don't be clever when there is no need.
* Preferentially don't name a function "ComputeCosine" when "cos" will
do: name value-producing functions for what they produce, not how.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?