Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const class reference return from function, what does const do?

Reply
Thread Tools

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

 
 
Jim Langston
Guest
Posts: n/a
 
      05-11-2006
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?


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      05-11-2006

"Rolf Magnus" <> wrote in message
news:e3uq9a$c50$02$...
> 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 )


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-11-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What to return -- object, reference or const reference Arv C++ 15 03-07-2008 09:15 PM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Is it best to return a class or a reference to a class? Tom C++ 5 09-13-2006 08:46 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57