Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ const member function that returns a const pointer.. But what type of const is the returned pointer?

Tags:

c++

constants

I apologize if this has been asked, but how do I create a member function in c++ that returns a pointer in the following scenerios: 1. The returned pointer is constant, but the junk inside can be modified. 2. The junk inside is constant but the returned pointer can be modified. 3. Neither the junk, nor the pointer can be modified.

Is it like so:

  1. int *const func() const
  2. const int* func() const
  3. const int * const func() const

All of the tutorials I've read don't cover this distinction.

Side note: If my method is declared const then the tutorials say that I'm stating that I won't modify the parameters.. But this is not clear enough for me in the case when a parameter is a pointer. Do my parameters need to be like:

a. void func(const int* const x) const;
b. void func(const int* x) const;
c. void func(const int* const x) const;

like image 490
Jor Avatar asked Oct 08 '10 07:10

Jor


People also ask

Can a const function return a pointer?

It depends on what is const . If the constness refers to the pointed object, yes it does. If you try to make the pointer itself const, it doesn't make sense as it will be ignored.

What does it mean to return a const type from a function?

If you say that a function's return value is const: const int g(); you are promising that the original variable (inside the function frame) will not be modified. And again, because you're returning it by value, it's copied so the original value could never be modified via the return value.

What is const * const in C?

const int* const is a constant pointer to constant integer This means that the variable being declared is a constant pointer pointing to a constant integer.

What is const pointer in C?

A constant pointer is one that cannot change the address it contains. In other words, we can say that once a constant pointer points to a variable, it cannot point to any other variable. Note: However, these pointers can change the value of the variable they point to but cannot change the address they are holding.


2 Answers

I don't know what book you have read, but if you mark a method const it means that this will be of type const MyClass* instead of MyClass*, which in its turn means that you cannot change nonstatic data members that are not declared mutable, nor can you call any non-const methods on this.

Now for the return value.

1 . int * const func () const

The function is constant, and the returned pointer is constant but the 'junk inside' can be modified. However, I see no point in returning a const pointer because the ultimate function call will be an rvalue, and rvalues of non-class type cannot be const, meaning that const will be ignored anyway

2 . const int* func () const

This is a useful thing. The "junk inside" cannot be modified

3 . const int * const func() const

semantically almost the same as 2, due to reasons in 1.

HTH

like image 182
Armen Tsirunyan Avatar answered Sep 29 '22 06:09

Armen Tsirunyan


Some uses of const don't really make much sense.

Suppose you have the following function:

void myFunction (const int value); 

The const tells the compiler that value must not change inside the function. This information does not have any value for the caller. It's up to the function itself to decide what to do with the value. For the caller, the following two function definitions behave exactly the same for him:

void myFunction (const int value); void myFunction (int value); 

Because value is passed by value, which means that the function gets a local copy anyway.

On the other hand, if the argument is a reference or a pointer, things become very different.

void myFunction (const MyClass &value); 

This tells the caller that value is passed by reference (so behind the screens it's actually a pointer), but the caller promises not to change value. The same is true for pointers:

void myFunction (const MyClass *value); 

We pass a pointer to MyClass (because of performance reasons), but the function promises not to change the value.

If we would write the following:

void myFunction (MyClass * const value); 

Then we are back int he first situation. myFunction gets a pointer, which is passed by value, and which is const. Since MyFunction gets a copy of the pointer value, it doesn't matter for the caller whether it is const or not. The most important thing is that myFunction can change the contents of value, because the pointer variable itself is const, but the contents in it isn't.

The same is true for return values:

const double squareRoot(double d); 

This doesn't make any sense. squareRoot returns a const double but since this is passed 'by value', and thus needs to be copied to my own local variable, I can do whatever I want with it.

On the other hand:

const Customer *getCustomer(char *name); 

Tells me that getCustomer returns me a pointer to a customer, and I am not allowed to change the contents of the customer.

Actually, it would be better to make the char-pointer-contents const as well, since I don't expect the function to change the given string:

const Customer *getCustomer(const char *name); 
like image 33
Patrick Avatar answered Sep 29 '22 08:09

Patrick