Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pointers vs. Getter Functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Captacha   Members   -  Reputation: 141

Like
0Likes
Like

Posted 20 January 2012 - 04:21 PM

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?

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9498

Like
0Likes
Like

Posted 20 January 2012 - 04:25 PM

You could mean a number of different things. Show code for your two alternatives.

#3 Telastyn   Crossbones+   -  Reputation: 3726

Like
0Likes
Like

Posted 20 January 2012 - 04:26 PM

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

#4 Captacha   Members   -  Reputation: 141

Like
0Likes
Like

Posted 20 January 2012 - 04:44 PM

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.

#5 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 20 January 2012 - 04:48 PM

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.

#6 Codarki   Members   -  Reputation: 462

Like
0Likes
Like

Posted 20 January 2012 - 09:49 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS