M
Matthias Kaeppler
Hi,
I was wondering why library implementors often
make getter functions return strings by value
(copies). For example, in boost::filesystem the
leaf() function returns an std::string by value.
So does Gnome::Vfs::FileInfo::get_name().
Isn't that unnecessary overhead? I could as well
return by reference to const-string and avoid
copying the string, right? That's what I always
do, and it works quite well.
Okay, for strings it's maybe not that of a
performance hit, but with more complex objects I'd
reckon it's a bad idea to return copies.
I'm asking because I have this scenario:
I have a class called File, which defines an
interface for accessing information about a file.
Its state is mostly defined by a
Gnome::Vfs::FileInfo object.
Many functions in the File interface are just
wrappers; they delegate calls to the encapsulated
FileInfo object.
One such example is a function get_name(), which
returns the name of a file. It's implemented like
this:
Glib::ustring File::get_name() const {
return m_finfo_ptr->get_name();
}
I previously wanted to implement it like this:
const Glib::ustring& File::get_name() const {
return m_finfo_ptr->get_name();
}
That however issues a warning, because I'm
referencing a temporary (FileInfo::get_name()
returns a copy of the name-string, that's actually
why this question came up).
But back to the first version:
Glib::ustring File::get_name() const {
return m_finfo_ptr->get_name();
}
How many copies are created here?
FileInfo::get_name() creates a copy, and I'm then
returning a copy of that copy, right? That's terrible!
Please, someone shed some light on this.
Regards,
Matthias
I was wondering why library implementors often
make getter functions return strings by value
(copies). For example, in boost::filesystem the
leaf() function returns an std::string by value.
So does Gnome::Vfs::FileInfo::get_name().
Isn't that unnecessary overhead? I could as well
return by reference to const-string and avoid
copying the string, right? That's what I always
do, and it works quite well.
Okay, for strings it's maybe not that of a
performance hit, but with more complex objects I'd
reckon it's a bad idea to return copies.
I'm asking because I have this scenario:
I have a class called File, which defines an
interface for accessing information about a file.
Its state is mostly defined by a
Gnome::Vfs::FileInfo object.
Many functions in the File interface are just
wrappers; they delegate calls to the encapsulated
FileInfo object.
One such example is a function get_name(), which
returns the name of a file. It's implemented like
this:
Glib::ustring File::get_name() const {
return m_finfo_ptr->get_name();
}
I previously wanted to implement it like this:
const Glib::ustring& File::get_name() const {
return m_finfo_ptr->get_name();
}
That however issues a warning, because I'm
referencing a temporary (FileInfo::get_name()
returns a copy of the name-string, that's actually
why this question came up).
But back to the first version:
Glib::ustring File::get_name() const {
return m_finfo_ptr->get_name();
}
How many copies are created here?
FileInfo::get_name() creates a copy, and I'm then
returning a copy of that copy, right? That's terrible!
Please, someone shed some light on this.
Regards,
Matthias