* const int f() and int f() const
@ 2006-03-19 15:18 Shriramana Sharma
2006-03-19 16:54 ` ab
2006-03-19 17:45 ` Steve Graegert
0 siblings, 2 replies; 7+ messages in thread
From: Shriramana Sharma @ 2006-03-19 15:18 UTC (permalink / raw)
To: Linux C Programming List
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:
const int f();
or
int f() const;
What is the difference? Thank you.
--
Tux #395953 resides at http://samvit.org
playing with KDE 3.51 on SUSE Linux 10.0
$ date [] CCE +2006-03-19 W11-7 UTC+0530
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: const int f() and int f() const
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
1 sibling, 1 reply; 7+ messages in thread
From: ab @ 2006-03-19 16:54 UTC (permalink / raw)
To: Shriramana Sharma, Linux C Programming List
> 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:
>
> const int f();
>
> or
>
> int f() const;
This one.
> What is the difference? Thank you.
The first one returns a 'const int.'
The second one is an immutable function.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: const int f() and int f() const
2006-03-19 16:54 ` ab
@ 2006-03-19 17:30 ` Robert P. J. Day
0 siblings, 0 replies; 7+ messages in thread
From: Robert P. J. Day @ 2006-03-19 17:30 UTC (permalink / raw)
To: ab; +Cc: Shriramana Sharma, Linux C Programming List
On Sun, 19 Mar 2006, ab 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:
> >
> > const int f();
> >
> > or
> >
> > int f() const;
>
> This one.
>
> > What is the difference? Thank you.
>
> The first one returns a 'const int.'
could you give an example where using that first declaration would
actually make a difference, given C's parameter-passing mechanism?
thanks.
rday
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: const int f() and int f() const
2006-03-19 15:18 const int f() and int f() const Shriramana Sharma
2006-03-19 16:54 ` ab
@ 2006-03-19 17:45 ` Steve Graegert
2006-03-19 23:59 ` Shriramana Sharma
2006-03-22 22:44 ` Benjamin Sobotta
1 sibling, 2 replies; 7+ messages in thread
From: Steve Graegert @ 2006-03-19 17:45 UTC (permalink / raw)
To: linux-c-programming
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).
> 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: const int f() and int f() const
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
1 sibling, 1 reply; 7+ messages in thread
From: Shriramana Sharma @ 2006-03-19 23:59 UTC (permalink / raw)
To: Linux C Programming List
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: const int f() and int f() const
2006-03-19 23:59 ` Shriramana Sharma
@ 2006-03-20 13:01 ` Glynn Clements
0 siblings, 0 replies; 7+ messages in thread
From: Glynn Clements @ 2006-03-20 13:01 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
Shriramana Sharma wrote:
> > 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
Putting "const" before the *return type* means that the return value
is a constant.
In that situation, "const" is just a normal type qualifier, with the
same meaning as when used in variable and parameter declarations.
> 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.
You can use *any* method on a mutable (non-const) instance.
The above indicates that one() returns a "const int", that's all.
> 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?
Wrong. It will not allow the method to be used on *const* instances,
as the method itself isn't declared as "const".
If a method doesn't modify the instance, you *should* declare it as
"const". There is no reason not to; const methods can be used on both
const and mutable instances, while methods which aren't declared const
can only be used on mutable instances.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: const int f() and int f() const
2006-03-19 17:45 ` Steve Graegert
2006-03-19 23:59 ` Shriramana Sharma
@ 2006-03-22 22:44 ` Benjamin Sobotta
1 sibling, 0 replies; 7+ messages in thread
From: Benjamin Sobotta @ 2006-03-22 22:44 UTC (permalink / raw)
To: Steve Graegert; +Cc: linux-c-programming
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
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2006-03-22 22:44 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).