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!
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'.
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).
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.
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.
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.
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.
You can use next = const_cast<blog *>(b)
const_cast will remove or cast away the constantness of the variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With