Aminos
Aminos

Reputation: 871

const in function prototype

If we have a function prototype like this :

const CString function(...)
{
CString x;
//do some stuff
return x;
}

does this mean that function returns a const CString ? because const in C++ can be placed in front of a method inside a class to tell the compiler that the attributes will not be modified (or not mutable)

I ask this "dumb" question, because in another case we can have something like this :

static CString function(...)
{ }

And in this case static relates to "function" and not to the variable returned.

Upvotes: 0

Views: 3581

Answers (6)

Faisal
Faisal

Reputation: 361

const appearing before the method describes the attributes of the return type of the method. So, const CString method_name(); means that the method returns a const CString object i.e. an object that cannot be modified by the caller of this method.

const appearing after the method describes the attributes of the method itself. It tells that the method does not "mutate" or "modify" the object it operates on.

So, const CString method_name() const; means that the method cannot modify any of the member variables of the object used to invoke the method. It has no relation with the return type of the method.

Let us see this with an example.

class Foo
{
  int x;
  int GetX() const {return x;}
  void SetX(int i_x)  {x = i_x;}  
};

In the above class definition, GetX() is a const method i.e. it is not expected to modify the object which is used to invoke them. What does this mean? When you declare a class, there is a single version of each of the member functions which gets loaded onto memory. Further, all the non-static member methods need an object to invoke them. How is this achieved? When you declare a non-static member method of the class, the C++ compiler slyly changes the signature of the methods to expect a "this" pointer, that is the pointer to the object used to invoke the method. For a non-const method, the type of the "this" pointer expected is Class*. So our SetX() method above looks like: void SetX(Foo* this, int i_x) {this->x = i_x;} You can use the this pointer to modify the object(x is part of the object right!) However, for a const method, the type of the "this" pointer passed is const Class* i.e. you cannot modify the object using the "this" pointer. So GetX becomes int GetX(const Foo* this) {return this->x; } Since the type of the this pointer is const Foo*, you cannot change the object the pointer points to i.e. you cannot do someething like: x = 100; which would translate to this->x = 100; which the compiler will complain about.

Upvotes: 1

DevSolar
DevSolar

Reputation: 70293

does this mean that function returns a const CString ?

Yes it does.

because const in C++ can be placed in front of a method inside a class to tell the compiler that the attributes will not be modified (or not mutable)

No it can't, the const has to be placed after the method to achieve that.

 class MyClass {
     // ...
     const CString function();
     // ...
 }

A function returning const CString.

 class MyClass {
     // ...
     CString const function();
     // ...
 }

Same thing.

 class MyClass {
     // ...
     CString function() const;
     // ...
 }

A function that can be called on a constant object, as it "promises" not to change internal state.



As a rule of thumb, const is always after the thing declared constant...

...except for the old C-style const <type>, kept for backward compatibility.

The following two lines are the same thing, a constant pointer to a constant int. Note that to make the pointer constant, the const has to be trailing the *:

const int * const p1; // the exception
int const * const p2;

Upvotes: 3

Jarod42
Jarod42

Reputation: 217448

const CString function(...) returns a const CString,

but it is deprecated as rarely useful and by the fact that disallows move on the temporary. So, in CString s; s = foo();, a copy is done instead of a move.

The most significant usage was to forbid things like

foo() = CString(..);

C++11 introduces ref qualifier on method and r-value reference which allows to fix that differently:

Obj& operator = (const Obj& rhs) & ; // Note the final &

Upvotes: 2

dbush
dbush

Reputation: 224082

A function declared as const CString function(...) returns a const CString. This the the same in C++ as it is in C.

If you have a C++ method defined like this:

void MyClass::function(void) cont;

That means that the method function does not modify the object.

Upvotes: 1

Wicked
Wicked

Reputation: 126

To return a const value from a function use :

const CString function(...)
{
    CString x;
    //do some stuff
    return x;
 }

To tell the compilater that attributes won't change use :

CString myClass::function(...) const
{
    CString x;
    //do some stuff
    return x;
 }

Upvotes: 2

NathanOliver
NathanOliver

Reputation: 180660

In C++ const return_type function_name(params) means you have a function that returns a const return_type. If you have static return_type function_name(params) then this marks the function as static and you no longer need an instance of the class to call this function. If you want to mark a function as const meaning that it will not moodify the class contents then you need to place the const after the function as

return_type function_name(params) const
                                  ^^^^^

Upvotes: 4

Related Questions