c++ : 关于函数重载
假如有以下类声明
#pragma once
#include <glm\glm.hpp>
#include <GL\glew.h>
class Vertex
{
public:
Vertex(const glm::vec3& pos)
{
this->pos = pos;
}
protected:
private:
glm::vec3 pos;
};
class Mesh
{
public:
Mesh(Vertex* vertices);
Mesh(Vertex* vertices, unsigned int numVertices);
void Draw();
virtual ~Mesh();
protected:
private:
Mesh(const Mesh& other);
void operator=(const Mesh& other);
enum
{
POSITION_VB,
NUM_BUFFERS
};
GLuint m_vertexArrayObject;
GLuint m_vertexArrayBuffers[NUM_BUFFERS];
unsigned int m_drawCount;
};
定义
#include "mesh.h"
#include "debug.h"
Mesh::Mesh(Vertex* vertices)
{
//bug::debug d;
debug_out(("Mesh(a) >>\n"));
debug_out((" sizeof(Vertex):%d\n", sizeof(Vertex)));
debug_out((" sizeof(glm::vec3):%d\n", sizeof(glm::vec3)));
debug_out((" sizeof(vertices):%d\n", sizeof(vertices)));
debug_out((" sizeof(vertices[0]):%d\n", sizeof(vertices[0])));
debug_out((" n:%d\n", sizeof(Vertex) / sizeof(vertices[0])));
Mesh(vertices, sizeof(vertices)-1);
}
Mesh::Mesh(Vertex* vertices, unsigned int numVertices)
{
debug_out(("Mesh(a,b) >>\n"));
debug_out((" numVertices:%d\n", numVertices));
debug_out((" sizeof(vertices[0]):%d\n", sizeof(vertices[0])));
m_drawCount = numVertices;
//VAO
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &m_vertexArrayObject);
}
void Mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, m_drawCount);
glBindVertexArray(0);
}
调用
// 设置三角形顶点
Vertex vertices[] = {
Vertex(glm::vec3(-0.5, -0.5, 0)),
Vertex(glm::vec3(0, 0.5, 0)),
Vertex(glm::vec3(0.5, -0.5, 0)),
};
//bug::debug d;
debug_out(("main >>\n"));
debug_out((" sizeof(Vertex):%d\n", sizeof(Vertex)));
debug_out((" sizeof(glm::vec3):%d\n", sizeof(glm::vec3)));
debug_out((" sizeof(vertices):%d\n", sizeof(vertices)));
debug_out((" sizeof(vertices[0]):%d\n", sizeof(vertices[0])));
debug_out((" n:%d\n", sizeof(vertices) / sizeof(vertices[0])));
Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
//Mesh mesh(vertices);
问题,在调用的时候,按2个参数实例化Mesh类,能出来正确结果,
Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
如果按1个参数来调用,没有出现正确结果
Mesh mesh(vertices);
按照原本的设想,希望能省一个参数,没有达到目的,不知问题出在哪?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mesh::Mesh(Vertex* vertices)
的形参是指针,调用sizeof
并不能得到数组的 size,而是得到指针的 size。所以sizeof(vertices) - 1
并不能得到你预期的值。