* (e-mail address removed):
I was just wondering if the return type of a function is
"std::list<const char *> &", then what could be the harm if I return
"std::list<char *>"?
That would allow you to change a constant object, like e.g.
char const* const s = "puck";
std::list<char*> varList;
std::list<char const*>& constList = varList; // !Not allowed
constList.push_back( s );
char* const p = *varList.begin();
p[0] = 'f';
All I am saying there is, I have a list of 'non-const' pointers,
please treat that as 'const' pointer and don't try to mess with the
content pointed.
To me, it makes complete sense and valid thinking. Not sure what am i
missing??
You're providing too little information to give any definite answer -- see the
FAQ about How To Post.
However, it does *seem* as if you're missing three important concepts and have
one misconception, none of them directly related to the 'const' issue:
* Probably missing, the concept of referring to something (via a pointer or
C++ reference doesn't matter in that respect). Reasons that it's probably
missing: having a list of raw pointers, returning a result by reference.
* Probably missing, the concept of object lifetime. Reason it's probably
missing: returning by reference.
* Probably missing, the concept of Return Value Optimization (RVO). Reason
it's probably missing: returning by reference.
* Probable misconception, that micro efficiency matters a lot. Reason it's
probably there: returning by reference, using a list of raw pointers.
If so the only cure is experience coupled with correct information (e.g. a good
book, or for that matter Usenet discussions).
But one doesn't always need to know the details of what's wrong in order to
suggest a cure.
In the only example you've given I suggest using std::list< std::string >, and
return that type directly (not tacking on a '&' at the end).
Cheers & hth.,
- Alf