K''
K''

Reputation: 5228

How to pass a constant pointer to a method in a class

I've this constructor of a Node class:

Node::Node(int item,  Node const * next)
{
    this->item = item;
    this->next = next;
}

When I compile it gives a compile error: invalid conversion from 'const Node*' to 'Node*'

Is there a way to pass a pointer pointing to constant data?

Upvotes: 3

Views: 16566

Answers (3)

shihongzhi
shihongzhi

Reputation: 1931

It also works with pointers but one has to be careful where ‘const’ is put as that determines whether the pointer or what it points to is constant. For example,

    const int * Constant2
declares that Constant2 is a variable pointer to a constant integer and

    int const * Constant2
is an alternative syntax which does the same, whereas

    int * const Constant3
declares that Constant3 is constant pointer to a variable integer and

    int const * const Constant4
declares that Constant4 is constant pointer to a constant integer. Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).

http://duramecho.com/ComputerInformation/WhyHowCppConst.html I think this link will help you.you need know what const mean.Good Luck.

Upvotes: 0

iammilind
iammilind

Reputation: 69988

Is there a way to pass a pointer pointing to constant data?

Yes. Use Node const* (or const Node*) as you have showed in your code.

To fix your compiler error, you have 3 choices:

  1. Node::Node() should receive a non-const pointer so that it can be assigned to this->next
  2. Change the design and declare Node::next also a Node const*
  3. Typecast, this->next = const_cast<Node*>(next);

The 3rd solution should be used with utmost care otherwise it may result in undefined behavior.

Upvotes: 0

Adam Rosenfield
Adam Rosenfield

Reputation: 400204

You're doing it correctly, but the compiler is right to complain: you're assigning a "pointer to a const Node" to a variable with a type of "pointer to a non-const Node". If you later modify this->next, you're violating the contract of "I will not modify the variable pointed to by next.

The easy fix is just to declare next as a pointer to non-const data. If the variable this->next will truly never be modified for the life of the Node object, then you can alternatively declare the class member to be a pointer to a const object:

class Node
{
    ...
    const Node *next;
}:

Also note the distinction between "pointer to const data" and "const pointer to data". For single-level pointers, there are 4 types of pointers in regards to their constness:

Node *ptr;  // Non-constant pointer to non-constant data
Node *const ptr;  // Constant pointer to non-constant data
const Node *ptr;  // Non-constant pointer to constant data
Node const *ptr;  // Same as above
const Node *const ptr;  // Constant pointer to constant data
Node const *const ptr;  // Same as above

Note that const Node is the same as Node const at the last level, but the placement of const with regards to the pointer declaration ("*") is very important.

Upvotes: 15

Related Questions