Andre Ahmed
Andre Ahmed

Reputation: 1889

Getting the address of a std::vector inside a struct

I'm using DirectX and I would like to access the vertices std::vector which are defined and assigned inside a struct.

struct Mesh
{
 std::<vector> mVertices;
}

I would like to get the address of the first element

vinitData.pSysMem = m_Mesh.m_Vertices[0];

I tried the above line but it doesn't work at all.

The only work around is I copied the vertices to another std:vector then assign it to pSysMem and that worked.

By doing like that:

std::vector<VertexAttribute> vertices(m_Mesh.m_Vertices.size());
    for (size_t i = 0; i < m_Mesh.m_Vertices.size(); ++i)
    {
        vertices[i].Pos = m_Mesh.m_Vertices[i].Position;

    }

Upvotes: 0

Views: 312

Answers (2)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42929

You could either get it by stating:

vinitData.pSysMem = &(m_Mesh.m_Vertices[0]);

or if your compiler supports C++11:

vinitData.pSysMem = const_cast<Vertix*>(m_Mesh.m_Vertices.data());

Upvotes: 1

Drakosha
Drakosha

Reputation: 12165

operator[] returns reference, not pointer. Use:

vinitData.pSysMem = &m_Mesh.m_Vertices[0];

BTW, i am assuming you use std::vector<Vertix>

Upvotes: 1

Related Questions