是 const_cast吗?有没有用过?

发布于 2024-10-22 12:20:01 字数 395 浏览 2 评论 0原文

最近我发现了一段C++代码,它有效地做了以下事情:

char* pointer = ...;
const char* constPointer = const_cast<const char*>( pointer );

显然作者认为const_cast的意思是“添加const”,但实际上const也可以添加隐式:

const char* constPointer = pointer;

是否有任何情况下我真的必须 const_cast 指向 const 的指针(const_cast 作为在上面的例子中)?

Recently I found a piece of C++ code that effectively does the following:

char* pointer = ...;
const char* constPointer = const_cast<const char*>( pointer );

Obviously the author thought that const_cast means "add const", but in fact const can be just as well added implicitly:

const char* constPointer = pointer;

Is there any case when I would really have to const_cast to a pointer-to-const (const_cast<const Type*> as in above example)?

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

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

发布评论

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

评论(4

青芜 2024-10-29 12:20:01

当你有 2 个重载并且你想要强制执行 const 一个时。当您用一个术语称呼另一个术语时,通常会出现这种情况。

class A
{
public:
   B* get();
   const B* get() const;
};

我有一个非常量 A 但想运行 get() const 我可能会强制转换。特别是我可能会在非常量本身的实现中这样做。

B* A::get() 
{
   return const_cast<B*>( const_cast< const A*>(this)->get() );
}

当然我们可以这样做:

B* A::get()
{
    const A* constthis = this; // no need to cast
    return const_cast<B*>(constthis->get());
}

所以我们不必进行强制转换,但这使得第一个解决方案成为单行解决方案,并且不需要创建临时变量。

Where you have 2 overloads and you want to force the const one to be executed. This is often the case when you call one in terms of the other.

class A
{
public:
   B* get();
   const B* get() const;
};

I have a non-const A but want to run get() const I might cast. In particular I might do this in the implementation of the non-const itself.

B* A::get() 
{
   return const_cast<B*>( const_cast< const A*>(this)->get() );
}

Of course we could do:

B* A::get()
{
    const A* constthis = this; // no need to cast
    return const_cast<B*>(constthis->get());
}

so we did not have to cast but it makes the first solution a one-liner and no need to create a temp variable.

沫雨熙 2024-10-29 12:20:01

const_cast 尽管有其名称,但并不特定于 const;它与cv-qualifiers一起使用,它有效地包含const易失性

虽然透明地允许添加这样的限定符,但删除任何限定符需要 const_cast

因此,在您给出的示例中:

char* p = /**/;
char const* q = const_cast<char const*>(p);

const_cast 的存在是虚假的(我个人认为它掩盖了语法)。

但是您可能希望删除易失性,在这种情况下您将需要它:

char const volatile* p = /**/;
char const* q = const_cast<char const*>(p);

例如,这可能会出现在驱动程序代码中。

const_cast, despite its name, is not specific to const; it works with cv-qualifiers which effectively comprises both const and volatile.

While adding such a qualifier is allowed transparently, removing any requires a const_cast.

Therefore, in the example you give:

char* p = /**/;
char const* q = const_cast<char const*>(p);

the presence of the const_cast is spurious (I personally think it obscures the syntax).

But you can wish to remove volatile, in which case you'll need it:

char const volatile* p = /**/;
char const* q = const_cast<char const*>(p);

This could appear, for example, in driver code.

jJeQQOZ5 2024-10-29 12:20:01

也许在有 f(T*) 和 f(const T*) 的情况下强制重载解析。

Maybe to force overload resolution in cases where you have f(T*) and f(const T*).

清眉祭 2024-10-29 12:20:01

您也可以使用static_cast来添加const。因此,我没有看到任何必须使用 const_cast 来添加 const 的情况。但是,当您想要更改对象的类型(例如为了重载解析)时,有时可能需要显式转换(无论是其中一种还是另一种)。

例如

void f(char*);
void f(const char*);

int main()
{
   char* p = 0;
   f(p); //calls f(char*)
   f(static_cast<const char*>(p)); //calls f(const char*);
   f(const_cast<const char*>(p)); //calls f(const char*);
}

You can use static_cast to add const as well. So I don't see any situation where you have to use const_cast to add const. But explicit casting (be it one or another) can sometimes be needed when you want to change the type of the object for example for overload resolution.

E.g.

void f(char*);
void f(const char*);

int main()
{
   char* p = 0;
   f(p); //calls f(char*)
   f(static_cast<const char*>(p)); //calls f(const char*);
   f(const_cast<const char*>(p)); //calls f(const char*);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文