473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Returning const references to class data

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
Jul 22 '05 #1
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-

Jul 22 '05 #2
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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 ,...
4
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...
18
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...
12
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...
19
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...
10
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. ...
17
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(){
3
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...
23
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.