• Advertisement
Sign in to follow this  

Pointers vs. Getter Functions

This topic is 2191 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

I'm wondering why people don't use pointers instead of getter functions. It's needs much less code, less memory and doesn't require a function call. The reason people use getter functions(at least from my knowledge) is because they want to create variables that can be used in the rest of their code without letting it be written to. Can someone explain to me what I'm missing here?

Share this post


Link to post
Share on other sites
Advertisement
You could mean a number of different things. Show code for your two alternatives.

Share this post


Link to post
Share on other sites
[quote name='Telastyn' timestamp='1327098389' post='4904702']
What makes you think that pointers disallow writing to a value?
[/quote]
Pointer allow you to change the value?
[Googles it] Didn't know about that. Sorry for the dumb question.

Share this post


Link to post
Share on other sites
Also how would that even look like? You have a private member and then a public const pointer you initialize in the constructor?

Edit: got ninjad on that one. To at least add to the topic. One of the big reasons you hide stuff behind getters/setters is also that you can change the internal structure without affecting the interface. Maybe at a later point your int data member gets moved down to a internal substructure or is packed into something else or changes it's type.

Share this post


Link to post
Share on other sites
[quote name='Captacha' timestamp='1327098115' post='4904698']
I'm wondering why people don't use pointers instead of getter functions. It's needs much less code, less memory and doesn't require a function call. The reason people use getter functions(at least from my knowledge) is because they want to create variables that can be used in the rest of their code without letting it be written to. Can someone explain to me what I'm missing here?
[/quote]
Using pointers or getter functions are not related to each other or exclusive. Needing more code is incorrect. Extra function call is 99% incorrect. Needing more memory is incorrect. The reason people use getter functions is becouse they want the class to be a stuct.

Don't use pointers if you don't want to think about memory management. References are better, lays out clear ownership structure. If you need getters for all data members, then it should be a struct or the design is flawed.

Share this post


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

  • Advertisement