当构造函数运行结束时指针变为空

发布于 2024-12-07 09:51:21 字数 857 浏览 0 评论 0原文

我有一个相机类,它有两个指针作为参数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2024-12-14 09:51:21

您按值传递 matProjection,因此,构造函数中对其的赋值会分配给本地副本(在外部不可见)。尝试使用参考:

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX* &matProjection, D3DMATRIX* &matView)
---------------^        ------------------^

代码的其余部分应保持不变。

对您的评论的回复:

我不太明白的一件事是为什么我不必放置“&”调用中matProjection和matView前面

因为当函数参数被声明为引用时,编译器会计算出来并传递一个引用(可能实现为指针)。

我是否正确地认为您传递了它指向的值..因此您需要引用来获取实际指针的地址?

是的。指针与 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:

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX* &matProjection, D3DMATRIX* &matView)
---------------^        ------------------^

The rest of the code should stay the same.

Responses to your comments:

one more thing I don't quite understand is why I don't have to place an '&' in front of matProjection and matView in the call

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.

am I right in thinking that you pass the value it points to.. therefore you need to reference is to get the address of the actual pointer?

Yes. Pointers are no different than ints, and under normal conditions, their value is passed. When you need the function to set your variable (of pointer type, but the same applies to ints), 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.

倦话 2024-12-14 09:51:21

您正在按值传递指针。因此,Camera 构造函数正在为这些指针的副本赋值。您需要通过引用传递指针......

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX*& matProjection, D3DMATRIX*& matView)

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...

Camera::Camera(
    float fOVDeg, float nearCull, float farCull,
    float xPos, float yPos, float zPos,
    D3DMATRIX*& matProjection, D3DMATRIX*& matView)
稍尽春風 2024-12-14 09:51:21

查看今天的另一个问题: 为什么我的指针在之后不为空免费?

您需要一个引用类型参数,或一个指向指针的指针。按值参数无法完成您的任务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文