Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: c++ public accessor method

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    3

    c++ public accessor method

    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

  2. #2
    Junior Member Newbie
    Join Date
    Mar 2012
    Location
    Angus
    Posts
    5

    Re: c++ public accessor method

    So you want a method that gets those values? Why not just make 3 public methods that return the float, like:

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

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    3

    Re: c++ public accessor method

    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.

    Code :
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •