Reputation: 17
So basically, I have a class called vertex2. It is a class I am making for using OpenGL easier (for me). So I made a constructor. It took in a float list. But here's the problem. I tried to make a variable that was a list of floats; it was float list[] = {1, 0, 0, 1, 1, 1, 0}
And i assigned a vertex2 and it worked. But I tried pasting that: {1, 0, 0, 1, 1, 1, 0}
in the initialization, but it didn't work.
struct vertex2
{
vertex2(float *vertices);
private:
float *m_vertices;
public:
float *ConvertToFloatArray();
};
static game::data::vertex2 vertices = { -0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f };
But if I do this:
static float verts[] = { -0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f };
static game::data::vertex2 vertices = verts;
it somehow works.
Upvotes: 0
Views: 89
Reputation: 1083
When you're doing:
struct vertex2
{
vertex2(float *vertices);
private:
float *m_vertices;
public:
float *ConvertToFloatArray();
};
static float verts[] = { -0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f };
static game::data::vertex2 vertices = verts;
You are declaring a static variable with the vertexes and passing a pointer to it on the constructor and (my guess, because you didn't included the complete code) saving the pointer in your object. If someone modifies vertices, the vertices on the class will get modified (and likewise, if you modify your vertex on the class, it will modify vertices variable).
But when you do:
static game::data::vertex2 vertices = { -0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f };
You're passing a list of floats, not a pointer.
Instead, i'll suggest you to play with this: https://ideone.com/GVvL8y
#include <array>
class vertex2 // use class whenever possible, not struct
{
public:
static const int NUM_VERTICES= 6;
public:
// Now you can only init it with a reference to a 6 float array
// If you use float* you'll never know how many floats are there
constexpr vertex2(float (&v)[NUM_VERTICES])
: vertices{v[0], v[1], v[2], v[3], v[4], v[5]}
{
}
// Will take a list of numbers; if there's more than 6
// will ignore them. If there's less than 6, will complete
// with 0
constexpr vertex2(std::initializer_list<float> list)
{
int i= 0;
for (auto f= list.begin(); i < 6 && f != list.end(); ++f) {
vertices[i++]= *f;
}
while (i < NUM_VERTICES) {
vertices[i++]= 0;
}
}
constexpr vertex2() = default;
constexpr vertex2(vertex2&&) = default;
constexpr vertex2(const vertex2&) = default;
float* ConvertToFloatArray() const;
// Just for debugging
friend std::ostream& operator<<(std::ostream& stream, const vertex2& v)
{
stream << '{' << v.vertices[0];
for (int i= 1; i < vertex2::NUM_VERTICES; i++) {
stream << ',' << v.vertices[i];
}
return stream << '}';
}
private:
std::array<float, NUM_VERTICES> vertices{};
};
Upvotes: 3
Reputation: 50
Make like this
vertex2 vertices{(float []){-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f }};
Compiler try to use initializer_list as default, you should clearly indicate this as array
Upvotes: -2