Re: A style question on const char* vs. std::string
Seungbeom Kim wrote:
Ulrich Eckhardt wrote:
Zeljko Vrba wrote:
class transaction_ticket {
private:
static titles_map& titles;
static titles_map& get_titles();
...
};
titles_map& transaction_ticket::titles = get_titles(); [*]
titles_map& transaction_ticket::get_titles()
{
static titles_map titles;
[...]
return titles;
}
This code is broken. [...]
Now, the problem here is that someone was trying to be smart and make
the
syntax a bit more convenient by simply binding the returnvalue of that
function in a reference. However, doing that he also introduced another
global object which again suffers the undefined initialisation order.
So, which exactly is the error? The fact that titles (the data member)
is a reference, not an object?
Presumably, the fact that he has declared the variable means
that he is using it (or intends to use it) somewhere, rather
than systematically calling get_titles. If that somewhere
happens to be in the constructor for a static object, of course,
he's hosed.
The reference that get_titles() returns is to a static object, which is
initialised at the first call to get_titles() and persists until the end
of the program, and titles (the data member) is a reference to it.. so I
can't see what could be a problem here.
In addition, I don't see any "global object" that you say he introduced,
even in class scopes; the only object I see is titles in a local scope.
Probably sloppy wording, but I've often seen "global object"
(and "static object") used to designate objects with static
lifetime, rather than objects with the corresponding scope. As
far as the standard is concerned, of course, global is only used
with regards to scope (i.e. the global namespace), and it is
probably better to limit it to this use in general. (And in the
case of static, always specify what is meant, since static in
C++ can mean so many different things.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]