There is a class in our code, say class C
. I want to create a vector of objects of class C
. However, both the copy constructor and assignment operator are purposely declared to be private
. I don't want to (and perhaps am not allowed) to change that.
Is there any other clean way to use/define vector<C>
?
You could use a vector<C*>
or vector<shared_ptr<C>>
instead.
No, it isn't, std::vector
requires assignable concept. The authors of C must have had a good reason to prohibit this, you have to stick with whatever they provide to copy/assign instances of C
. Either you use pointers as suggested above, or C
provides other mechanism to copy/assign itself. In the latter case you could write an assignable proxy type for C
.
Do you have access to the boost library?
Create a vector of boost shared pointers.
std::vector<boost:shared_ptr<C>>
I have managed to do it by using just two friends:
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
friend class std::vector;
template<typename _T1, typename _T2>
friend void std::_Construct(_T1* __p, const _T2& __value);
Put them inside your class declaration and voila!
I am using gcc 5.3.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With