2

I am writing a new class using OpenGL, i have two possibilities for my constructor :

VertexObject();
VertexObject(GLuint* vertices,GLuint* elements);

What i would like to do is that VertexObject() calls the other one with an already inisialised array such as

    VertexObject::VertexObject() : 
    VertexObject( 
    (GLuint[]) {
        0, 1, 2,
        2, 3, 0
    },
    (GLuint[]) {
        0, 1, 2,
        2, 3, 0
    }) {}

But it seems C++ won't let me do it, error being 'taking address of temporary array'. I am not even sure what i am asking for is doable but any help will be greatly appreciated.

2
  • Shouldn't vertices and elements be pointers-to-const? Commented Jun 16, 2014 at 13:16
  • @Simple Because of non-constance, I think the solution is a bit more complicated - see my answer Commented Jun 16, 2014 at 15:11

3 Answers 3

2

I propose you to look at boost library, particularly assign pattern(facilitates initialization of containers) might help.

Here is a small code snippet that might give you grasp of idea:

VertexObjectc(boost::assign::list_of(0)(1)(2)(2)(3)(0).convert_to_container<GLuint>() );

I haven`t tested it.

Sign up to request clarification or add additional context in comments.

2 Comments

The other answer solved my problem but the boost library actually triggered another error : couldn't convert from int to the other type. I don't really have the time to fully understand it but now i know about Boos which seems like a good library. Thanks a lot for the help.
@ user3714670 Your are welcome. Boost worth to be study(at least partly), it provides a lot of great functionality.
1

If you deep copy the array in the constructor or if the arrays are never ever modified and VertexObject doesn't take the ownership of the pointers, this should work:

GLuint def_vert[6] = { // static storage
    0, 1, 2,
    2, 3, 0
};
VertexObject::VertexObject() : 
VertexObject(def_vert, def_vert) {}

You can use separate arrays if you want different values for each parameter of course.

3 Comments

Huh i don't know why i didn't think about that, i already use that for the default_shaders, thanks a lot it solved it ^^
It can be wrong - the parameter of the constructor is not const GLuint *, but GLuint *, so def_vert can be changed. See my answer.
@ikh: yes, if the constructor doesn't modify the array, then the parameter should be declared const for the sake of const-correctness. In that case def_vert can be const as well.
0

Try this:

// VertexObject.h
class VertexObject
{
private:
    static const GLuint m_default_vertices[6], m_default_elements[6];
    static GLuint *GetDefVertices();
    static GLuint *GetDefElements();
public:
    VertexObject() : VertexObject(GetDefVertices(), GetDefElements()) { }
    ...
};
// VertexObject.cpp
const GLuint VertexObject::m_default_vertices[6] = {
    0, 1, 2,
    2, 3, 0
};
const GLuint VertexObject::m_default_elements[6] = {
    0, 1, 2,
    2, 3, 0
};
GLuint *VertexObject::GetDefVertices()
{
    static thread_local GLuint ar[6];
    memcpy(ar, m_default_vertices, sizeof(ar));
    return ar;
}
GLuint *VertexObject::GetDefElements()
{
    static thread_local GLuint ar[6];
    memcpy(ar, m_default_elements, sizeof(ar));
    return ar;
}

(http://ideone.com/IvujSq)

Note that it works on C++11.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.