将参数传递给 const 参数时出现问题

发布于 2024-08-15 11:02:06 字数 472 浏览 1 评论 0原文

假设我有一个函数需要对指针进行常量引用...

示例:

void Foo( const Bar *&p_Thing, );

并且我将指针传递

Bar *blah = NULL; // Initialized when program starts up

给该函数

Foo( blah );

我可能会遇到这样的编译器错误

invalid initialization of reference of type 'const Bar*&' from expression of type 'Bar*'

这在我身上发生过几次,我真的很想阐明 const 在应用于与参数传递相关的参数方面的操作方式。如有任何帮助,我们将不胜感激,谢谢。

Say I have a function that takes a const reference to a pointer...

Example:

void Foo( const Bar *&p_Thing, );

and I pass a pointer

Bar *blah = NULL; // Initialized when program starts up

to the function

Foo( blah );

I may encounter a compiler error like this

invalid initialization of reference of type 'const Bar*&' from expression of type 'Bar*'

This has happened to me a few times, and I'd really like to clear up how const operates in terms of applying to parameters in relation to argument passing. Any help is appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

北方。的韩爷 2024-08-22 11:02:06

这就是您想要的:

void Foo( Bar * const &p_Thing );

然后它成为对 Bar * 指针的 const 引用,它具有编译的可爱功能。

This is what you want:

void Foo( Bar * const &p_Thing );

Then it becomes a const-reference to a Bar * pointer, which has the lovely feature of compiling.

筱武穆 2024-08-22 11:02:06

我假设您想要对 Bar* 的 const 引用,而不是对 const Bar* 的引用?如果是,您想将其定义为 void Foo(Bar* const& p)

I assume you want a const reference to a Bar*, not a reference to a const Bar*? If yes you want to define it as void Foo(Bar* const& p)

欢烬 2024-08-22 11:02:06

我永远无法弄清楚指向引用的指针和指向指针的引用之间的语法差异。所以我消除了歧义:

typedef const Bar *PBar;
void Foo(const PBar &p_Thing);

这样你也可以消除“const 指针”/“指向 const 的指针”的歧义。

I could never figure the syntactic difference between pointers to references and references to pointers. So I disambiguate:

typedef const Bar *PBar;
void Foo(const PBar &p_Thing);

This way you also get rid of the "const pointer"/"pointer to const" ambiguity.

冰火雁神 2024-08-22 11:02:06

这种特殊情况下的响应很简单:操作期望引用指向 const 内存的指针,但您向其传递了指向非常量内存的指针。将 blah 设为 const Bar* 就可以了。

但是,如果您在指针和常量方面遇到问题,则应该考虑此传递方案。通过引用传递指针并不罕见,但对于 90% 的任务来说,这并不是真正必要的。您可以传递指针或引用,或者将其作为函数或方法调用的值返回。就内存管理和代码的易读性而言,这也会更容易。

The response in this particular case is easy: the operation is expecting a reference to a pointer to const memory, but you're passing it a pointer to a non-const memory. Making blah a const Bar* would allow it.

However, you should consider this passing scheme if you're having trouble with pointers and constness. Passing a pointer by reference is not uncommon, but for 90% of the tasks it is not really necessary. You could pass the pointer, or a reference, or return it as a value of the function or method call, instead. That would be easier in terms of memory management and legibility of your code too.

熊抱啵儿 2024-08-22 11:02:06

您的函数需要一个指向常量数据指针

您正在传递一个指向可变或非常量数据的指针。一些编译器可能会抱怨目标数据的常量性。

Your function is requiring a pointer to constant data.

You are passing a pointer to mutable or non-constant data. Some compilers may complain about the constness of the target data.

如梦亦如幻 2024-08-22 11:02:06

虽然大多数问题确实回答了这个问题(您不能通过引用将变异指针传递到通过引用获取非变异指针的函数中),但我认为任何答案都没有明确说明其基本原理:

它会破坏 const -正确性。如果通过引用将非可变指针传递给通过引用获取常量指针的函数,它很可能会修改该指针以引用常量对象(毕竟,该类型保证它不会修改该值)。现在的问题是,在函数之外,传入的指针实际上不是 const,编译器必须允许对指向的对象进行任何变异操作,这意味着修改最初是常量的对象。

这是一个简短的例子:

const int y = 0;
void f( const int *& p ) {
   p = &y; // ok, p is a pointer to a constant integer
}
int main()
{
   int *p;
   f( p ); // if this was allowed p would point to y, but y is constant
   *p = 5; // correct, p is a mutating pointer
}

While most questions do answer the question (you cannot pass a mutating pointer by reference into a function that takes a non-mutating pointer by reference), I don't think the rationale is made explicit in any of the answers:

It would break const-correctness. If you pass a non-mutating pointer by reference to a function taking a constant pointer by reference, it could well modify the pointer to refer to a constant object (after all, the type guarantees that it will not modify that value). Now the problem is that outside of your function the pointer passed in is actually not const, and the compiler would have to allow any mutating operation on the pointed object, and that would imply modifying an object that was initially constant.

Here is a short example:

const int y = 0;
void f( const int *& p ) {
   p = &y; // ok, p is a pointer to a constant integer
}
int main()
{
   int *p;
   f( p ); // if this was allowed p would point to y, but y is constant
   *p = 5; // correct, p is a mutating pointer
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文