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.
精彩评论