Pointers vs. Getter Functions

Started by
4 comments, last by Codarki 12 years, 3 months ago
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?
Advertisement
You could mean a number of different things. Show code for your two alternatives.
What makes you think that pointers disallow writing to a value?

What makes you think that pointers disallow writing to a value?

Pointer allow you to change the value?
[Googles it] Didn't know about that. Sorry for the dumb question.
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.

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?

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.

This topic is closed to new replies.

Advertisement