Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a class object as an argument in C++

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)?

like image 928
Steve Avatar asked Dec 29 '22 03:12

Steve


1 Answers

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();
    }
    // ...
};
like image 67
Jim Brissom Avatar answered Jan 09 '23 23:01

Jim Brissom