1

Can you load structs into a vbo where the member variables are private?

UPDATE

The question was left generic with no sample because I was not sure how to ask the specific questions. The answer below gets right to the point of what I was looking for. But for those that wast to see the code that spurred the question:

#ifndef POINT2D_H_INCLUDED
#define POINT2D_H_INCLUDED

#include <glext.h>

namespace glext 
{
  /*! \class Point 2D class geometry based
   *  \brief This class defines a 2D point_2d
   *
   * Some details about the Test class
   */
  template <typename T>
  class point_2d
  {
  private:
    /// The first element in the ordered pair
    T _x;

    /// The second element in the ordered pair
    T _y;

  public:
    /*! \brief default constructor sets both elements in ordered pair to 0
     */  
    point_2d();

    /*! \brief constructor sets both elements to the paramaters provided
     */  
    point_2d(const T &x, const T &y);

    /*! \brief copy constructor sets both elements in ordered pair to 0
     */  
    point_2d(const point_2d &rhs);

    /*! \brief destructor 
     */
    ~point_2d();

    /*! \brief assignment uses copy-swap idiom 
     */ 
    point_2d &operator=(point_2d rhs);

    /*! \brief setter for x element
     */ 
    void x(const T &x);

    /*! \brief setter for y element
     */
    void y(const T &y);

    /*! \brief setter for both x and y element
     */
    void x_and_y(const T &x, const T &y);

    /*! \brief swizzle for both x and y returns a copy of a point_2d
     */
    point_2d xy() const;

    /*! \brief swizzle for x element returns a copy of a point_2d
     */
    point_2d xx() const;

    /*! \brief swizzle for y element returns a copy of a point_2d
     */
    point_2d yy() const;

    /*! \brief swizzle for reverse of y and x returns a copy of a point_2d
     */
    point_2d yx() const;

    /*! \brief getter for x element returns a reference to x of type T
     */
    T& x() const;

    /*! \brief getter for y element returns a reference to y of type T
     */
    T& y() const;
  };

  template <typename T>
  void swap(point_2d<T> &lhs, point_2d<T> &rhs);

  template <typename T>
  bool operator<(const point_2d<T> &lhs, const point_2d<T> &rhs);

  template <typename T>
  bool operator>(const point_2d<T> &lhs, const point_2d<T> &lhs);

  template <typename T>
  bool operator==(const point_2d<T> &lhs, const point_2d<T> &rhs);

  template <typename T>
  bool operator!=(const point_2d<T> &lhs, const point_2d<T> &rhs);
}
#include "type_point_2d_2d.inl"
#endif
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140
  • Does the structure have member functions for retrieving the data or is it plain old data? Would be nice to see how the data structure is declared. – Captain Obvlious May 25 '13 at 20:33

1 Answers1

7

This really has nothing to do with OpenGL or buffer objects. What you're asking is what the result of copying the binary data of a struct with private members is. That is, what you get with memcpy.

C++98/03 only allows such memory layout to be legal if the object is a POD (plain old data) type. Classes with private members aren't PODs.

C++11 relaxes these rules. For memcpy to work (which is what glBufferSubData ultimately does), you need the type to be trivially copyable. So no virtual functions or non-trivial members. In order to ensure the actual layout of the data (so that offsetof will actually work, for example), the type must be standard layout.

Specifically for you, this means that if you have private members, then all of the (non-static) member variables must be private. You lose standard layout if some members are public and others are private.

Community
  • 1
  • 1
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982