开发者

difference between using extern and using a getter?

开发者 https://www.devze.com 2023-04-09 14:39 出处:网络
I have a camera class that sets up two variables - a projection and a view matrix. I could either make t开发者_Python百科hese extern and include the header in anything I wanted to have access to the v

I have a camera class that sets up two variables - a projection and a view matrix. I could either make t开发者_Python百科hese extern and include the header in anything I wanted to have access to the variables, or I could make a getter for them / pass references for these variables to anything that needed them. Is this simply a case of preference or are there real benefits to doing it a particular way?


A getter has the advantage of not letting external code write in your variable (eg a pointer array can't be incremented).

Otherwise, it's functionally the same, especially if it's for your own use.


It is more a question of encapsulation. If your camera class is the only holder of these two matrices, it would be wise to make them available via getters and not with public attributes.

This will, for example, allow you to subclass your camera and create other methods for your view and projection matrices.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号