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)?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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.
so there are two way of passing class object(it is what you are asking) as a function argument i) Either pass the copy of object to the function, in this way if there is any change done by the function in the object will not be reflected in the original object
ii) Pass the base address of the object as a argument to the function.In thsi method if there are any changes done in the object by the calling function, they will be reflected in the orignal object too.
for example have a look at this link, it clearly demonstrate the usage of pass by value and pass by reference is clearly demonstrated in Jim Brissom answer.
Hope this helps.