When writing a code this way...
#include<bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node(int data) {
this->data = data;
}
};
void display(const Node* &p){
cout<<p->data<<endl;
}
int main(){
Node *p = new Node(10);
display(p);
return 0;
}
I get the following error.
error: cannot bind non-const lvalue reference of type 'const Node*&' to an rvalue of type 'const Node*'
However, this works if the parameter const Node* &p
of function display
is changed to Node* &p
.
Can someone explain to me how this works?