Hello,
I'm wondering if returning const references to a class member is
generally faster than returning a copy of the object.
Consider this code:
class A
{
std::string data;
public:
const std::string& get_data() const { return data; }
}
versus
class B
{
std::string data;
public:
std::string get_data() const { return data; }
}
If I now have this code:
// ...
A a;
B b;
std::string str = a.get_data();
std:.string str = b.get_data();
Which assignment will be faster? My first impression was, that since (I
guess) data is not copied in A::get_data but returned by reference, we
save one call of a copy constructor. But is this also the case in
"reality"? Which version would you prefer?
Thanks,
Matthias 2 1653
matthias_k wrote: Hello,
I'm wondering if returning const references to a class member is generally faster than returning a copy of the object. Consider this code:
class A { std::string data; public: const std::string& get_data() const { return data; } }
versus
class B { std::string data; public: std::string get_data() const { return data; } }
If I now have this code:
// ... A a; B b;
std::string str = a.get_data(); std:.string str = b.get_data();
Which assignment will be faster? My first impression was, that since
(I guess) data is not copied in A::get_data but returned by reference,
we save one call of a copy constructor. But is this also the case in "reality"? Which version would you prefer?
Thanks, Matthias
For built-in types (or small user-defined types), class B will probably
be faster, although if inlining is performed, they might probably be
the same.
For expensive user-defined types (e.g., require allocation etc), class
A will probably be faster, although if inlining (and heavy
optimisation) is performed, they might probably be the same. But
usually for these types, its best to return by const reference.
-shez-
shez wrote: matthias_k wrote:
Hello,
I'm wondering if returning const references to a class member is generally faster than returning a copy of the object. Consider this code:
class A { std::string data; public: const std::string& get_data() const { return data; } }
versus
class B { std::string data; public: std::string get_data() const { return data; } }
If I now have this code:
// ... A a; B b;
std::string str = a.get_data(); std:.string str = b.get_data();
Which assignment will be faster? My first impression was, that since
(I
guess) data is not copied in A::get_data but returned by reference,
we
save one call of a copy constructor. But is this also the case in "reality"? Which version would you prefer?
Thanks, Matthias
For built-in types (or small user-defined types), class B will probably be faster, although if inlining is performed, they might probably be the same.
For expensive user-defined types (e.g., require allocation etc), class A will probably be faster, although if inlining (and heavy optimisation) is performed, they might probably be the same. But usually for these types, its best to return by const reference.
-shez-
Yes, I should have clarified that I was only concerned about complex
types. But basically that's what I thought as well, thanks shez.
Regards,
Matthias This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pablo J Royo |
last post by:
Hello:
i have a function that reads a file as an argument and returns a reference
to an object that contains some information obtained from the file: FData
&ReadFile(string FilePath);
But ,...
|
by: Patrick |
last post by:
I want to achieve the following behaviour but I am having trouble
getting there...bare with me im knew to c++ , so its probably rather
trivial!
To have a class ClassA, and composed within this...
|
by: cppaddict |
last post by:
Hi,
Is it considered bad form to have the subscript operator return a
const reference variable? If not, what is the proper way to do it?
My question was prompted by the code below, my...
|
by: Olumide |
last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is
an absolutely fantastic book; however on page 175 (topic: operator
overlaoding), there the following code snippet:
inline...
|
by: Steven T. Hatton |
last post by:
I believe it is technically possible to return a pointer to the first
element of an array. I can persuade the returned pointer to act like an
array, with some qualifications. I'm specifically...
|
by: Fraser Ross |
last post by:
I need to know the syntax for writing a reference of an array. I haven't
seen it done often. I have a class with a member array and I want a member
function to return an reference to it. ...
|
by: djcredo |
last post by:
Hey all,
I want to return a pointer to a struct. Here is what I'lm trying to do:
struct Position{
int x;
int y;
};
Position* GraphicTag::getRenderCentre(){
|
by: Belebele |
last post by:
I have an Element class which is abstract and I would like to have an
object of the Iterator class to iterate over a range of elements.
I would like to use std::for_each to instrument the...
|
by: pauldepstein |
last post by:
Below is posted from a link for Stanford students in computer
science.
QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |