linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Sobotta <mayday@gmx.net>
To: Steve Graegert <graegerts@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: const int f() and int f() const
Date: Wed, 22 Mar 2006 23:44:50 +0100	[thread overview]
Message-ID: <200603222344.50947.mayday@gmx.net> (raw)
In-Reply-To: <6a00c8d50603190945u2c66339agbdb43c023a725097@mail.gmail.com>

On Sunday 19 March 2006 18:45, Steve Graegert wrote:
> On 3/19/06, Shriramana Sharma <samjnaa@gmail.com> wrote:
> > Suppose there is a function f() which returns an integer and does not
> > change any value of the class it belongs to, should I declare it as:
>
> I don't understand what you mean here.
>
> > const int f();
>
> By declaring a const return type you are promising that the original
> variable (inside the function's stack frame) will not be modified.
> Because you're returning it by value, it's copied so the original
> value could never be modified via the return value.  This qualifier
> has not meaning for primitive builtin types.  This is different for
> user-defined types: if a function returns a class object by value as
> const, the return value of that function cannot be an lvalue (that is,
> it cannot be assigned to or otherwise modified).

It can have a meaning for built in types, too. For example you could do 
something like this:

int& f(...) {....}

Through this you could actually change a value inside the class. Or cause a 
segfault - depending on what you return, a member or a temp variable :)
Constructs of this kind are only usable of you pass back a reference to a 
member that lives beyond the function call, of course. If you use const you 
make sure that your reference is read-only.

>
> > or
> >
> > int f() const;
>
> 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.
>
> You probably want to choose the latter one.
>
> 	\Steve
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

      parent reply	other threads:[~2006-03-22 22:44 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
2006-03-20 13:01     ` Glynn Clements
2006-03-22 22:44   ` Benjamin Sobotta [this message]

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=200603222344.50947.mayday@gmx.net \
    --to=mayday@gmx.net \
    --cc=graegerts@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).