简体   繁体   English

引用对象内部的指针

[英]Referencing a pointer inside an object

I asked a question similar to this awhile ago which answered my question perfectly, but I have a new dilemma which I don't fully understand and would appreciate it if someone could help. 我问了一个类似不久前的问题,这个问题很好地回答了我的问题,但是我有一个新的难题,我不完全理解,如果有人可以帮助,我将不胜感激。 I have the following variable in a class: 我在课堂上有以下变量:

ID3D11Buffer* variable

I need to de-reference this variable in my draw call for DirectX so I create a function to do so: 我需要在对DirectX的绘制调用中取消引用此变量,因此我创建了一个函数来这样做:

const ID3D11Buffer* GetBuffer() const { return &variable; }

But then I get this error which I don't really understand and it's kinda hard to google: 但是然后我收到了一个我不太了解的错误,对Google来说有点难:

error C2440: 'return' : cannot convert from 'ID3D11Buffer *const *' to 'const ID3D11Buffer *' 错误C2440:'返回':无法从'ID3D11Buffer * const *'转换为'const ID3D11Buffer *'

Could a wise C++ guru enlighten me please? 明智的C ++专家能启发我吗? :D :D

Edit: With the answer provided I get a new error, this was why I was trying to add the & at the start, how would I make this line valid? 编辑:有了提供的答案,我得到一个新的错误,这就是为什么我尝试在开始时添加&的原因,我将如何使此行有效?

The draw call line: 抽奖电话:

md3dImmediateContext->IASetVertexBuffers(0, 1, cube.GetBuffer(), &stride, &offset);

The answer below creates this new error: 下面的答案会创建此新错误:

error C2664: 'ID3D11DeviceContext::IASetVertexBuffers' : cannot convert parameter 3 from 'const ID3D11Buffer *' to 'ID3D11Buffer *const *' 错误C2664:'ID3D11DeviceContext :: IASetVertexBuffers':无法将参数3从'const ID3D11Buffer *'转换为'ID3D11Buffer * const *'

The original code I had to make this work was 我要做这项工作的原始代码是

md3dImmediateContext->IASetVertexBuffers(0, 1, &buffer, &stride, &offset);

Edit # 2: Thanks for the help, your suggestion made the get function valid, but the IASetVertexBuffers function still didn't like it. 编辑#2:感谢您的帮助,您的建议使get函数有效,但是IASetVertexBuffers函数仍然不喜欢它。 It returns this error with the new suggestion: 它使用新建议返回此错误:

error C2664: 'ID3D11DeviceContext::IASetVertexBuffers' : cannot convert parameter 3 from 'const ID3D11Buffer *const *' to 'ID3D11Buffer *const *' 错误C2664:'ID3D11DeviceContext :: IASetVertexBuffers':无法将参数3从'const ID3D11Buffer * const *'转换为'ID3D11Buffer * const *'

GetBuffer should return a pointer to the buffer - not a pointer (to pointer) to it. GetBuffer应该返回一个指向缓冲区的指针-而不是指向它的指针。 Since variable is already defined as a pointer, you can simply return that: 由于variable已经定义为指针,因此您可以简单地返回它:

const ID3D11Buffer* GetBuffer() const { return variable; }

&variable is of type ID3D11Buffer** which doesn't match a pointer of type ID3D11Buffer* , hence the error. &variable的类型为ID3D11Buffer** ,与ID3D11Buffer*类型的指针不匹配,因此出现错误。

Edit - 编辑-

After reading the edit made to the OP's post, it is clear that GetBuffer is supposed to return a pointer (to pointer) to the buffer. 读取对OP帖子所做的编辑后,很明显, GetBuffer应该返回一个指向缓冲区的指针(指向该指针)。 So what was in fact wrong was the return type , not the return value. 因此,实际上错误的是返回类型 ,而不是返回值。 So he simply needs to change the return type signature to: 因此,他只需要将返回类型签名更改为:

const ID3D11Buffer* const* GetBuffer const { return &variable; }
//                ^^^^^^^^

and return what he originally did, which was the address of the pointer variable . 并返回他最初所做的事情,这就是指针variable的地址。

variable is already of the type ID3D11Buffer* . variable已经是ID3D11Buffer*类型。 So returning return variable; 因此,返回return variable; should suffice. 应该足够了。

look at const X * const 看看const X * const

If you draw a line at the * 如果您在*处画一条线

The const on the left refers to the pointer, so you can't change the pointer value (where it points) 左侧的const指向指针,因此您无法更改指针值(它指向的位置)

the const on the right refers to the object being pointed to, so you can't change the contents 右侧的const指向所指向的对象,因此您无法更改其内容

A little trick to find out which is const (pointer or variable) you can read it backwards. 找出哪个是const(指针或变量)的小技巧,您可以向后阅读。

  • const type* X; const type * X; -> Pointer to a variable of type 'type' which is const (the data is const) ->指向“类型”类型为const的变量的指针(数据为const)
  • type * const X; 输入* const X; -> const pointer to a variable of type 'type' (the pointer is const) ->指向类型为'type'的变量的const指针(指针为const)
  • const type * const X; const type * const X; -> const pointer to a variable of type 'type' which is const (data and pointer are const) ->指向类型为“ type”的变量的const指针,该变量为const(数据和指针均为const)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM