passing vector : reference or value?

Hi,

I have this:
I want to pass a vector of Base class, to MyClass and in MyClass resize and use further.

class Base
{
public:
.....
.....
};

class MyClass
{
public:
MyClass( vector <Base> _Base_ ); //1st situation
{
base = _Base_;
base.resize(10);
..... assign stuff .....
}
MyClass( vector <Base> *_Base_ ); //2nd situation
{
base = _Base_;
(*base).resize(10);
..... assign stuff .....
}
private:
vector <Base> base; //1st situation
vector <Base> *base; //2nd situation
};

main()
{
vector <Base> base;

MyClass *myclass = new MyClass( base ); //1st situation
MyClass *myclass = new MyClass(&base ); //2nd situation
}


vector is already a dynamic link list. Generally pass by reference is always preferable. But in case of vectors, is there any benefit of situation 2 over situation 1 ??

Is this the way of standard/efficient programming?

Thanks.

--
eye51
Pass by reference. In this case, I would pass by const reference (you are passing by pointer, which is probably just as efficient but is a lot messier).

The benefit is that you don't have to copy a vector which could be huge multiple times.
Hi Firedraco,

Thanks for quick reply. Do you mean to say this ? :


class MyClass
{
public:
MyClass( vector <Base> & _Base_ ); //1st situation
{
base = _Base_;
base.resize(10);
}

private:
vector <Base> base; //1st situation
};

main()
{
vector <Base> base;

MyClass *myclass = new MyClass( base ); //1st situation
}

Thanks.

--
eye51
I personally would be very careful of holding a reference (or pointer) to an external object inside a class object. It would very much depend on the overall design. It would be good to see why you want to do this.
Hi Galik,

Thanks for comments.

Actually this is to be applied to a parallel framework. Let say, MyClass will be called on each CPU with 4 copies of Base vector and assigned some data from other file IO functions.

I will create a MyClass with Base class as reference on each CPU and in each CPU, different copies of Base class will be created. Then these will be performing the computations independently on each CPU for each copy.

I hope this is the correct way of doing it.

Thanks.

--
eye51
I can't say I completely understand but it seems to me you want to apportion the processing of a vector to multiple processors. If that is the case then passing in a reference to the vector will pass the whole data to all processors. I don't really get exactly what you are doing but I suppose each class object will have a reference to the vector and will be set to iterate over a different portion. Is that right?

Well if any of that is close then using a reference does make more sense than copying portions into each object. Your class is being used more as a processing object rather than for data encapsulation. That is one of the cases where passing in a reference makes sense.

You might want to look up functors (function objects). They could be of use to you here.

http://www2.research.att.com/~bs/bs_faq2.html#function-object

Also you might consider putting iterators into your object/functor rather than the vector itself. That way each object could be given iterators covering a different range of the whole vector.
I'm coming in without reading any of the above but Disch wrote a nice little post on this: http://www.cplusplus.com/forum/beginner/27107/#msg144955
Topic archived. No new replies allowed.