Viktor Axén
Viktor Axén

Reputation: 319

How do I create a struct for storing and returning an array of floats?

I want to create a struct that holds texture coordinates for a square texture. The struct should have only one static member which is a constant array of 8 floats, and only one function, which returns the array.

I tried this:

struct TextureCoordinates
{
    static constexpr GLfloat m_texturecoords[8] = {
            1.0f, 0.0f,
            1.0f, 1.0f,
            0.0f, 1.0f,
            0.0f, 0.0f,
    };

    GLfloat* const gettexcoords() { return &m_texturecoords; }
};

but I get an error saying that the return type doesn't match function type. How do I change this struct to make it work in a memory efficient way?

Upvotes: 2

Views: 135

Answers (1)

Yang
Yang

Reputation: 8170

GLfloat* const means the GLfloat pointer is const, i.e., the pointer not the value it points is const. From https://isocpp.org/wiki/faq/const-correctness#const-ptr-vs-ptr-const:

Read the pointer declarations right-to-left.

const X* p means “p points to an X that is const”: the X object can’t be changed via p.

X* const p means “p is a const pointer to an X that is non-const”: you can’t change the pointer p itself, but you can change the X object via p.

const X* const p means “p is a const pointer to an X that is const”: you can’t change the pointer p itself, nor can you change the X object via p.

You need to return const GLfloat*. Since the method does not belong to a particular object, it can be static.

static const GLfloat* gettexcoords() { return m_texturecoords; }

Demo: http://ideone.com/6f1enU.

Upvotes: 3

Related Questions