Jump to content
  • Advertisement
Sign in to follow this  
Thirthe

[C++] readonly class members?

This topic is 4026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!