当构造函数运行结束时指针变为空
我有一个相机类,它有两个指针作为参数:
Camera::Camera(
float fOVDeg, float nearCull, float farCull,
float xPos, float yPos, float zPos,
D3DMATRIX* matProjection, D3DMATRIX* matView)
{
this->SetCamera(fOVDeg, nearCull, farCull);
this->AdjustCamera(xPos, yPos, zPos);
matProjection = &(this->projection);//gets the addresses of two private members
matView = &(this->view);
}
这是调用它的代码:
D3DMATRIX* matProjection,* matView;
//called once
void Initialise(HWND hWnd)
{
camera = new Camera(
45.0f, 1.0f, 100.0f,
0.0f, 9.0f, 24.0f,
matProjection, matView);
...rest of code...
基本上问题是我希望调用相机构造函数的代码中的两个指针保留相机类的内存地址两个私有会员,这样我就可以和他们一起做事!问题是,这工作正常,但是一旦构造函数完成,指针就会变成空(0x00000000)。我不知道为什么!相机的私有成员仍然具有值,因为我之前只是用吸气剂获取它们的值并且效果很好。
I have a camera class that has two pointers as parameters:
Camera::Camera(
float fOVDeg, float nearCull, float farCull,
float xPos, float yPos, float zPos,
D3DMATRIX* matProjection, D3DMATRIX* matView)
{
this->SetCamera(fOVDeg, nearCull, farCull);
this->AdjustCamera(xPos, yPos, zPos);
matProjection = &(this->projection);//gets the addresses of two private members
matView = &(this->view);
}
and this is the code that calls it:
D3DMATRIX* matProjection,* matView;
//called once
void Initialise(HWND hWnd)
{
camera = new Camera(
45.0f, 1.0f, 100.0f,
0.0f, 9.0f, 24.0f,
matProjection, matView);
...rest of code...
basically the problem is that I want the two pointers in the code that calls the camera constructor to retain the memory addresses of the camera classes two private members so I can do stuff with 'em! trouble is, this works fine, but as soon as the constructor is finished the pointers become null (0x00000000). I have no idea why! The camera's private members still have values because I was just grabbing their values with getters before and it worked fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您按值传递
matProjection
,因此,构造函数中对其的赋值会分配给本地副本(在外部不可见)。尝试使用参考:代码的其余部分应保持不变。
对您的评论的回复:
因为当函数参数被声明为引用时,编译器会计算出来并传递一个引用(可能实现为指针)。
是的。指针与 int 没有什么不同,在正常情况下,它们的值会被传递。当您需要函数来设置变量(指针类型,但同样适用于int)时,您必须传递一个指针到指针或一个引用到指针,这相似,但后者带有简洁的语法糖。
You are passing
matProjection
by value, therefore, the assignments to it in the constructor assign to a local copy (which is not visible outside). Try using a reference:The rest of the code should stay the same.
Responses to your comments:
Because when the function parameter is declared as a reference, the compiler figures that out and passes a reference (probably implemented as a pointer) instead.
Yes. Pointers are no different than
int
s, and under normal conditions, their value is passed. When you need the function to set your variable (of pointer type, but the same applies toint
s), you have to either pass a pointer-to-pointer or a reference-to-pointer, which are similar, but the latter comes with neat syntactic sugar.您正在按值传递指针。因此,Camera 构造函数正在为这些指针的副本赋值。您需要通过引用传递指针......
You are passing the pointers in by value. So the Camera constructor is assigning values to copies of those pointers. You need to pass the pointers in by reference...
查看今天的另一个问题: 为什么我的指针在之后不为空免费?
您需要一个引用类型参数,或一个指向指针的指针。按值参数无法完成您的任务。
See another today's question: Why is my pointer not null after free?
You need a reference type argument, or a pointer to pointer. By-value arguments won't do your task.