* operator! and lvalue
@ 2007-06-27 3:31 Shriramana Sharma
2007-06-28 9:42 ` Glynn Clements
0 siblings, 1 reply; 2+ messages in thread
From: Shriramana Sharma @ 2007-06-27 3:31 UTC (permalink / raw)
To: Linux C Programming List
In Thinking in C++ chapter on overloading operators, Bruce Eckel gives
the following code:
For a non-member operator:
int operator!(const Integer& a) {
cout << "!Integer\n";
return !a.i;
}
For a member operator:
Byte operator!() const {
cout << "!Byte\n";
return Byte(!b);
}
My question is: why is there no "const" before Integer and Byte at the
head of the function? There's all that stuff about when temporaries are
returned they are automatically const, but throughout the examples in
this chapter, only operator! is not explicitly specified to have a const
return value whereas in all other relevant cases we have the "const"
specified explicitly.
Shriramana Sharma.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: operator! and lvalue
2007-06-27 3:31 operator! and lvalue Shriramana Sharma
@ 2007-06-28 9:42 ` Glynn Clements
0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2007-06-28 9:42 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> In Thinking in C++ chapter on overloading operators, Bruce Eckel gives
> the following code:
>
> For a non-member operator:
>
> int operator!(const Integer& a) {
> cout << "!Integer\n";
> return !a.i;
> }
>
> For a member operator:
>
> Byte operator!() const {
> cout << "!Byte\n";
> return Byte(!b);
> }
>
> My question is: why is there no "const" before Integer and Byte at the
> head of the function?
Huh? The only "Integer" which isn't preceded by "const" is in the
string literal.
> There's all that stuff about when temporaries are
> returned they are automatically const, but throughout the examples in
> this chapter, only operator! is not explicitly specified to have a const
> return value whereas in all other relevant cases we have the "const"
> specified explicitly.
Why would you want to make the return value "const"?
The situation is different if you're referring a reference or pointer,
but there's no point in making a copied value "const".
BTW, this list is specifically about C programming on Linux; generic
C/C++ questions aren't really on-topic here. The comp.lang.c and
comp.lang.c++ newsgroups (and their *.moderated counterparts) might be
better places to ask such questions; certainly, they are likely to
have many more active users.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-06-28 9:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-27 3:31 operator! and lvalue Shriramana Sharma
2007-06-28 9:42 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).