So, I'm a pretty experienced C programmer who needs a lot of C ++ programming. There are some subtleties regarding a language that I never felt confident in. For example, the best methods for passing arguments.
Suppose, for example, that I have a class with a black box Object (it can have many member variables for everyone we know) and a function fn that takes an instance vector of Object as an argument. It seems to me that there are four main ways to convey this:
void fn(vector<Object> vec); void fn(vector<Object*> vec); void fn(vector<Object> *vec); void fn(vector<Object> &vec);
And, of course, we could also use some combination of these functions.
I want to make sure that I have this directly:
Method 1 must copy a vector class that includes a copy of each instance of the object in the vector. This could potentially be a huge overload and therefore bad.
(this one I'm not sure) Method 2 copied all the variables of the vec method, but it would only copy the addresses of each instance of the object. I don’t know enough about what is contained in the vector class to find out whether it is advisable or not.
Methods 3 and 4 are quite simple and similar to each other and introduce minimal overhead.
Is this all right? and what is the preferred method, bearing in mind that we know nothing about the object?
source share