Skip to contentSkip to conversation

const int f() and int f() const

DISCUSSION Shriramana Sharma 6 participants 7 messages Mar 19, 2006 – Mar 22, 2006

lore
Share & tools
lore
#0 lore raw

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.

Signature · 3 lines
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
#1 lore raw
Quoted 2 lines

Suppose there is a function f() which returns an integer and does not

change

Quoted 7 lines

any value of the class it belongs to, should I declare it as:

const int f();

or

int f() const;

This one.

Quoted 1 line

What is the difference? Thank you.

The first one returns a 'const int.'
The second one is an immutable function.

#2 lore raw
Quoted 9 lines · ab
Quoted 2 lines

Suppose there is a function f() which returns an integer and does not

change

Quoted 6 lines

any value of the class it belongs to, should I declare it as:

const int f();

or

Quoted 7 lines
Quoted 1 line

int f() const;

This one.

Quoted 1 line

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

#3 lore raw
In reply to Shriramana Sharma
Quoted 2 lines · Shriramana Sharma

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.

Quoted 1 line

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).

Quoted 3 lines

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

#4 lore raw

Thanks to all who replied.

Quoted 2 lines

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?

Quoted 4 lines

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?

Signature · 3 lines
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
#5 lore raw
Quoted 11 lines · Shriramana Sharma
Quoted 2 lines

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?

Quoted 4 lines

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.

Quoted 6 lines

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.

Quoted 4 lines

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.

Quoted 3 lines

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.

Signature · 1 line
Glynn Clements <[email protected]>
#6 lore raw
In reply to Steve Graegert
Quoted 8 lines · Steve Graegert
Quoted 2 lines · Shriramana Sharma

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.

Quoted 1 line

const int f();

Quoted 8 lines

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.

Quoted 17 lines
Quoted 4 lines

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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html