Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid conversion? Assigning a const pointer to a pointer

Tags:

c++

blog blog::operator+(const blog * b)
{
    next = b; //blog * next; it's a next pointer in a doubly linked list.
    return * this;
}

I was just wondering if there was any way I could assign b to next pointer of the current node. Is there a different way that I could do it? Right now I'm getting

invalid conversion from ‘const blog*’ to ‘blog*’ [-fpermissive]

Looking back on my notes, it seems to suggest that I should use constants while doing operator overloading, but right now it isn't really compiling. How would I be able to cast the const blog * b into next pointer? Thanks!

like image 243
ZZPLKF Avatar asked May 23 '13 06:05

ZZPLKF


People also ask

How do you assign a pointer to a constant to a pointer?

In the above code: We declare a pointer to constant. First, we assign the address of variable 'a' to the pointer 'ptr'. Then, we assign the address of variable 'b' to the pointer 'ptr'. Lastly, we try to print the value of 'ptr'.

Is it possible to assign a constant to a pointer variable?

1) If you cast a pointer of some type to a pointer of another type, you cannot cast pointer-to-const to pointer-to-non-const. 2) If you have constant pointer, the same rules applies as to other constants - you can assign a constant to a variable but you cannot assign a variable to a constant (except initializing it).

Which constant value can be assigned to a pointer variable?

Pointer to constant As the name itself indicates, the value of the variable to which the pointer is pointing, is constant. In other words, a pointer through which one cannot change the value of the variable to which it points is known as a pointer to constant.

Can a const pointer be modified?

const int * is a pointer to an integer constant. That means, the integer value that it is pointing at cannot be changed using that pointer.


3 Answers

From your question and comments I understand you are confused about the meaning of const blog *.

You have to read the pointer types backwards.

  • blog * is a pointer to blog - you may change the pointer value, you may change the pointed-to object with the pointer.
  • blog const * (and const blog *) is a pointer to const blog - you may change the pointer value, but you can't change the object with this pointer.
  • blog * const a const pointer to blog - you can't change the pointer value, but you still can change the pointed-to object with this pointer.

The confusion often arrises because you have two types that come to play here. The type of pointer and the pointed-to type. Both can be const.

With the interface of your operator+ you guarantee the object pointed to by the parameter will not get changed by that pointer (pointer to const blog). But then you assign it to a pointer to blog, which then could be used to alter the object and break the interface contract. I suggest you rethink the interface or change the implementation of your operator - you might want to copy the contents of the pointed-to object into the internal list, instead of just manipulating with pointers.

like image 200
Fiktik Avatar answered Oct 28 '22 13:10

Fiktik


If you really want to cast const pointer to non-const pointer you can use const_cast as Gaurav suggested.

However, I would use something like that only as a last resort when I really cornered by someone else's lib/api I just have to use.

If this is happened in your own code this ususally mean that you are not designed your api/interface carefully and/or misuse/misunderstand "const correctness"

I strongly enourage you to read about const correctness.

UPDATE: const corectness is not a just another fancy thing we have in C++. It may add some pain sometimes, but it also provides some room for assumptions to clients of your code as well as to compiler/optimizer. Doing const_cast literally "breaks the rules" and may easily lead to unexpected behavior.

like image 37
Eugene Loy Avatar answered Oct 28 '22 14:10

Eugene Loy


You can use next = const_cast<blog *>(b)

const_cast will remove or cast away the constantness of the variable.

like image 1
Daemon Avatar answered Oct 28 '22 15:10

Daemon