473,387 Members | 1,897 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,387 software developers and data experts.

const class reference return from function, what does const do?

I'm a little confused. I have a class function declared like:

const CItem& operator << (const std::string &sIn);

Which I use like this:

CItem Item;
Item << "blaster01,1,0,...,0,0,20,0,0,20,eol";

At the end of the method it does:
return *this;

My question is, is my const on the return value doing anything? I don't
know why I even have it there (maybe I copied this method from some source).

const CItem& seems to be to define a reference to a CItem that is constant.
Is that correct? Item is not declared as const, however. Maybe with the
way I'm using this thing I don't need to return any value at all anyway?
May 11 '06 #1
2 1766
Jim Langston wrote:
I'm a little confused. I have a class function declared like:

const CItem& operator << (const std::string &sIn);

Which I use like this:

CItem Item;
Item << "blaster01,1,0,...,0,0,20,0,0,20,eol";

At the end of the method it does:
return *this;

My question is, is my const on the return value doing anything?
Well, you won't be able to chain the operator calls, like:

Item << "Hello, " << "world";

Basically, returning the reference at all doesn't make much sense if it's
const.
I don't know why I even have it there (maybe I copied this method from
some source).

const CItem& seems to be to define a reference to a CItem that is
constant.
Is that correct?


Yes.

May 11 '06 #2

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:e3*************@news.t-online.com...
Jim Langston wrote:
I'm a little confused. I have a class function declared like:

const CItem& operator << (const std::string &sIn);

Which I use like this:

CItem Item;
Item << "blaster01,1,0,...,0,0,20,0,0,20,eol";

At the end of the method it does:
return *this;

My question is, is my const on the return value doing anything?


Well, you won't be able to chain the operator calls, like:

Item << "Hello, " << "world";

Basically, returning the reference at all doesn't make much sense if it's
const.
I don't know why I even have it there (maybe I copied this method from
some source).

const CItem& seems to be to define a reference to a CItem that is
constant.
Is that correct?


Yes.


Okay, so this begs the question, should I even be overriding the operator <<
anyway? If I'm not returning anything, maybe it would be better to just
make a function like

void LoadFromString( const std::string& sIn )
May 11 '06 #3

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

Similar topics

19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
3
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
23
by: grubertm | last post by:
When accessing pointer member variables these are not treated as const even though the instance is const. This came as a complete surprise to me and I would appreciate some feedback why that is the...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.