繁体   English   中英

将此类传递给引用的正确语法是什么?

[英]What's the correct syntax for passing this class to a reference?

说我有两个班:

class Foo
{
public:
    int x;
};

class Bar
{
public:
    Foo *foo;
};

一个实例如下:

Bar *bar;
bar = new Bar();

以及通过引用获取Foo对象的函数:

void func(Foo &foo);

使用bar中的foo调用此函数的正确方法是什么,如bar-> foo?

正确的语法是:

func(*(bar->foo));

bar->foo指向 bar->foo指针 我们只需要取消引用该指针:

Foo& f = *bar->foo;
func(f);

或者(一体化)

func(*bar->foo);

暂无
暂无

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

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