[C++] readonly class members?

Started by
2 comments, last by Thirthe 16 years, 9 months ago
why can't one simply do this and expects for it to work:
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; };

};
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.
Advertisement
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:

    int getData() const { return data; }    Node *getNext() const { return next; }


Please define what you mean by read-only (since, by definition, functions returning non-references are read-only members) and what you mean by not working.
actually, it works here as well.
lol, sorry for the thread.

This topic is closed to new replies.

Advertisement