PDA

View Full Version : c++ public accessor method



Przt014
04-19-2012, 09:11 PM
Hi,

I have three private member variables for a camera class.
Example

float m_FOV, m_Near, m_Far;

Now what I'm trying to create a public accessor method for them. Any ideas??

Thanks

wikaman1
04-20-2012, 04:22 AM
So you want a method that gets those values? Why not just make 3 public methods that return the float, like:


float camera::Get_m_FOV()
{
return m_FOV;
}

niobe
04-20-2012, 02:20 PM
Generally accessor functions are use to keep the internal structure of the class encapsulated. Considering that fact, I think it should be fine if you simply make your class a struct with those variables.
The reason you would use a struct instead of a class is simply good programming practice. Since a struct is a data structure(a group of data elements under one name).

If you wanted to create one accessor function for all these data elements, you could do it is follows. Although I have not seen it done like this very often, some people like wrapping things up.



void your_class_name::triple_var_accessor_func(float& var1, float& var2, float& var3)
{
var1 = m_FOV;
var2 = m_Near;
var3 = m_Far;
}


This is called "passing by reference" in case you are not familiar with it.