传递一个类对象在C参数++ [英] Passing a class object as an argument in C++

查看:231
本文介绍了传递一个类对象在C参数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有包含的用于显示的数据主要是数据和酒吧类的类名为foo。所以,如果我有富命名foobar的对象实例,我将如何将它传递到巴::显示()?喜欢的东西无效吧::显示(foobar的&放大器;测试)?

Suppose I had a class named foo containing mostly data and class bar that's used to display the data. So if I have object instance of foo named foobar, how would I pass it into bar::display()? Something like void bar::display(foobar &test)?

推荐答案

是的,差不多。或者,如果可能的话,使用const引用以表示该方法不会修改作为参数传递的对象。

Yes, almost. Or, if possible, use a const reference to signal that the method is not going to modify the object passed as an argument.

class A;

class B
{
    // ...
    void some_method(const A& obj)
    {
        obj.do_something();
    }
    // ...
};

这篇关于传递一个类对象在C参数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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