From: Shriramana Sharma <samjnaa@gmail.com>
To: Linux C Programming List <linux-c-programming@vger.kernel.org>
Subject: Re: const int f() and int f() const
Date: Mon, 20 Mar 2006 05:29:47 +0530 [thread overview]
Message-ID: <200603200529.47782.samjnaa@gmail.com> (raw)
In-Reply-To: <6a00c8d50603190945u2c66339agbdb43c023a725097@mail.gmail.com>
Thanks to all who replied.
> By declaring a const return type you are promising that the original
> variable (inside the function's stack frame) will not be modified.
But then this function would have to return the exact same value for each and
every time it is called, no?
> Marks a function as const allowing it to be called by const objects
> (btw, const objects can only call const member functions). This
> construct is usually found as part of member functions declarations,
> which are known to be immutable.
OK so int f() const means function f() does not change any members of the
class foo of which it is a member, and therefore it is safe for constant
instances of the class foo to call this function f().
OTOH, just putting const *before* the function name only means that the
*output* of the function is a constant value, say for example the function
const int one() { return 1; }
and it does not assure the compiler that it is okay for a *non-const* instance
of class foo to call this function.
In this case, the compiler will *not* accept calls to the function one() as a
member of a *non-const* instance of foo, even though the function does not
change any member of foo, right?
Is that right?
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-20 W12-1 UTC+0530
next prev parent reply other threads:[~2006-03-19 23:59 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-19 15:18 const int f() and int f() const Shriramana Sharma
2006-03-19 16:54 ` ab
2006-03-19 17:30 ` Robert P. J. Day
2006-03-19 17:45 ` Steve Graegert
2006-03-19 23:59 ` Shriramana Sharma [this message]
2006-03-20 13:01 ` Glynn Clements
2006-03-22 22:44 ` Benjamin Sobotta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200603200529.47782.samjnaa@gmail.com \
--to=samjnaa@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).