将对象转换为引用? [英] Casting an object to a reference?

查看:163
本文介绍了将对象转换为引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在读一些OSS代码,偶然发现这个特殊的部分:

I've been reading some OSS code lately and stumbled upon this peculiar piece:

class Foo { ..... };
void bar() {
    Foo x;
   Foo *y=new Foo();
   x=(const Foo &) *y;
}

在我的生活中,我找不到有关铸造行为的文档一个对象到一个const引用。

For the life of me I can't find documentation about the behavior of casting an object to a const reference.

推荐答案

x =(const Foo&)* y; / code>是赋值。我看到显式转换为const引用的唯一原因是 Foo :: operator =(const Foo&)调用赋值 Foo: :operator =(Foo&)也存在。

x=(const Foo &) *y; is assignment. The only reason I see to explicitly cast to const reference is to have Foo::operator=(const Foo &) called for assignment if Foo::operator=(Foo &) also exists.

这篇关于将对象转换为引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆