Sign in to follow this  
captacha

Pointers vs. Getter Functions

Recommended Posts

captacha    141
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
captacha    141
[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
japro    887
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
Ftn    462
[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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this