class Node{
int data;
Node *next;
public:
Node(Node *node_new){ data= node_new->data; next= NULL; };
~Node(){};
void setNext(Node *node_new){ next= node_new; };
int getData(){ return data; };
int *getNext(){ return next; };
};
[C++] readonly class members?
why can't one simply do this and expects for it to work:
see the last 2 members: how can i make them read-only?
note that
int getData() const { return data; };
int *getNext() const { return next; };
does not work.
Quote:Original post by Thirthe
why can't one simply do this and expects for it to work:
*** Source Snippet Removed ***
see the last 2 members: how can i make them read-only?
note that
int getData() const { return data; };
int *getNext() const { return next; };
does not work.
const int getData() { return data; };
const int *getNext() { return next; };
will make them return a "read-only" int and a pointer to a "read-only" int, respectively. That doesn't make the member functions themselves read-only. Member functions are already read-only.
The following Works HereTM:
Please define 1° what you mean by read-only (since, by definition, functions returning non-references are read-only members) and 2° what you mean by not working.
int getData() const { return data; } Node *getNext() const { return next; }
Please define 1° what you mean by read-only (since, by definition, functions returning non-references are read-only members) and 2° what you mean by not working.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement