Exactly.
The line under where you wrote "// I get compiler warnings on this:" is the line you want. No idea why it gives you warnings - it shouldn''t.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
[C++] operator overloading and memory management.
I thought that the const reference scoping rules only applied to function parameters, not return values, and only so that you could pass temporaries to functions expecting const references:
#include using std::string;void dosomething(const string& s){ // ...}int main(){ dosomething(string("hello")); return 0;}
quote:Original post by null_pointer
I thought that the const reference scoping rules only applied to function parameters, not return values, and only so that you could pass temporaries to functions expecting const references:
Not sure what you mean by "scoping rules", but I have member functions return objects by const reference all the time.
class ConstSingleton{ ConstSingleton (); // private property, keep outpublic: const ConstSingleton &getInstance () const { static ConstSingleton theObj; return theObj; }};
Doesn''t have to be singleton of course, but almost anywhere I return an object that''s guaranteed to exist, I return it by const reference. (If it might not exist, I return a pointer and return NULL when it doesn''t exist).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement