在构造函数中,我可以获取成员的地址并依靠它们将保持不变
In a constructor can I take the address of members and rely that they will remain unchanged?
我有这样一行代码:
boneHierarchy = BoneHierarchy(Point3D(24.5f,
84.0f + HumanBoundingBox::HEIGHT/2.0f ,24.5f
),
CompassRadians(0.0f),
renderingEngine
);
BoneHierarchy有一些向量作为私有成员。 我获取向量的地址并将它们传递给渲染类。 但是当我调用向量的大小成员函数时,我得到了垃圾。 我正在获取向量的地址,而不是其中的元素。
BoneHierarchy::BoneHierarchy(Point3D const& position,
CompassRadians const& heading,
Renderer* renderingEngine) : counter(0), renderingEngine(renderingEngine)
{
CoordinateSystem bodyCoordSys = CoordinateSystem(
Vector3D(0.0f,1.0f,0.0f),
Vector3D(0.0f,0.0f,1.0f));
map<Normal3D::enumeration, TextureCoordinates> cubeTextureKind;
Normal3D::enumeration normal;
for (normal = Normal3D::begin(); normal != Normal3D::end(); ++normal)
{
cubeTextureKind[normal] = TextureAtlas::textureAtlasCoordLookup(WOOD);
}
Bone body = Bone(
Vector3D(0.0f, HumanBoundingBox::HEIGHT/2.0f,0.0f),
HumanBoundingBox::DIM,
Euler3D(0.0f,0.0f,0.0f),
cubeTextureKind,
bodyCoordSys);
//TODO there are two positions for the human, one here and one in mesh vbo, unify these into one.
body.setPosition(Vector3D(position.getX(),position.getY(),position.getZ()));
body.setEuler(Euler3D((float) (Math::toDegrees(heading.getValue())),0.0f,0.0f));
drawBone(body,UPDATE_ALL);
root = body;
map<string,Renderer::Buffer> vboVariableMap;
vboVariableMap["aVertexPosition"] = Renderer::Buffer(verticesBuf);
vboVariableMap["aTextureCoord"] = Renderer::Buffer(texCoordsBuf);
vboVariableMap["index"] = Renderer::Buffer(indicesBuf);
cout << "aBoneIndex " << boneIndexBuf.size() << endl;
vboVariableMap["aBoneIndex"] = Renderer::Buffer(boneIndexBuf);
vboVariableMap["uBoneMatrix0"] = Renderer::Buffer(transformMatrixBuf);
GLuint textureID = 0;
Renderer::VBODescription vboDescription = Renderer::VBODescription("boneShader",
vboVariableMap,
GL_DYNAMIC_DRAW,
indicesBuf.size(),
textureID);
bufferID = renderingEngine->registerBuffer(vboDescription);
cout << "bufferID " << bufferID << endl;
renderingEngine->printBoneIndexSize();
cout << &(getBoneIndexBuf()) << endl;
}
Renderer::Buffer::Buffer(vector<GLfloat> const& data):
floatData(&data), ushortData(NULL), target(GL_ARRAY_BUFFER)
{
printf("pointer: %p, size: %lun", floatData, floatData->size());
}
Renderer::Buffer::Buffer(vector<GLushort> const& data):
floatData(NULL), ushortData(&data), target(GL_ELEMENT_ARRAY_BUFFER)
{
}
如果接受Point3D
和CompassRadians
的 BoneHierarchy
构造函数计算其自己的向量成员的地址,那很好。他们的地址不会改变。
该代码还显示赋值语句。右侧临时BoneHierarchy
的向量成员的地址将与左侧存储在boneHierarchy
中的BoneHierarchy
对象的地址不同。确保你没有复制赋值运算符中的地址,否则boneHierarchy
对象最终将引用不再存在的临时对象的向量成员。您还需要检查复制构造函数。
他们的地址不会改变。此外,如果您不在初始值设定项列表中,则其内容应在整个构造过程中有效。
这里提供的代码不足。假设您只想创建一个实例,请改用以下代码行:
BoneHierarchy boneHierarchy(Point3D(24.5f, 84.0f + HumanBoundingBox::HEIGHT/2.0f ,24.5f),
CompassRadians(0.0f), renderingEngine);
这将在本地堆栈中创建一个实例。正如 rob 指出的那样,您必须调用 BoneHierarchy 的赋值运算符。
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- ReadProcessMemory() 不适用于像 0x2840C6C68D8 这样的长地址
- CUDA:统一内存和指针地址的更改
- 当我们从/tp地址中添加/减去一个整数时会发生什么
- 在构造函数中,我可以获取成员的地址并依靠它们将保持不变