Reputation: 855
I understand that the correct way to declare a return type const is to write:
int foo() const;
But in some cases this doesn't work for me, then I can use:
const int foo();
What is the difference between these, and why can I use the latter in all cases but not the first one.
An example of where I can't use the first one is when I want to return an array of bools that are defined outside of the method.
Kind regards,
/Markus
Upvotes: 2
Views: 192
Reputation: 2038
int foo() const;
says that foo()
as a member function won't modify the data members of the class.
const int foo();
says that foo()
is a function whose return value is a const int
(an int
indeed).
Upvotes: 3
Reputation: 254471
int foo() const;
This does not declare the return type const
. It declares that it's a const
member function: it can be called on a const
object, and is not allowed to modify any (non-mutable
) members of the object.
const int foo();
This declares that the return type is const
; but there's not much point in doing that.
Upvotes: 1
Reputation: 165
First,
int foo() const;
and
int foo();
are a pair of functions you may define as a class member function. Their return type are all int.
When a const object of that class call function foo(), the first one will response.
This function
const int foo();
can be define everywhere. the return type is const int, so you must assign the function to a const int variable.
Upvotes: 2
Reputation: 153919
The const
modifies what is immediately before it; in
int foo() const;
it is the function which is const, not the return value. (By
definition, a function is const if the type of its this
pointer is a
pointer to const. Only member functions can be const.)
To make the return type const, place the const
immediately after the
return type, e.g.:
int const foo();
Note however that this const is ignored for non-class types; the return value will be an rvalue (a temporary), and only class type rvalues have cv qualified types. (I think some compilers will even warn about the above.)
Upvotes: 1
Reputation: 19349
Only the second example declares the return type as const. So if you want to return a const value, that's the way to do it (and the only way I believe)
The first snippet states that the this
pointer in a non-static member function points to a const object (i.e. the method will not modify the object).
Upvotes: 2
Reputation: 46943
The first one means that the method will not change any class member variables (directly or indirectly). This basically means that no class member can be assigned in this method and only <method> () const
can be called in the body of the method.
The second one means that the returned value cannot be changed.
Upvotes: 1
Reputation: 2362
The former one is used in class's method function. It means this method function doesn't (logically) change the data of the object.
Upvotes: 2
Reputation: 500357
I understand that the correct way to declare a return type const is to write:
int foo() const;
Your understanding is not correct. What the above does is declare foo()
as a const
member function. This has nothing whatsoever to do with foo()
's return type.
For more info, see What are the semantics of a const member function?
Upvotes: 6