There is a third option if you can modify Base You could make the vector protected which will allow base classes full access: class Base { ... protected: std::vector v; } Your base class can now directly operate on v If you need to expose much of the vector functionality to Derived this is the easiest way If not, I would just add the one function (i.e. Option #1) and would make it protected: class Base { ... protected: void push_back(const X &x) { v. Push_back(x); } private: std::vector v; }.
There is a third option if you can modify Base. You could make the vector protected which will allow base classes full access: class Base { ... protected: std::vector v; }; Your base class can now directly operate on v. If you need to expose much of the vector functionality to Derived, this is the easiest way.
If not, I would just add the one function (i.e. Option #1) and would make it protected: class Base { ... protected: void push_back(const X &x) { v. Push_back(x); } private: std::vector v; }.
Changing from private to protected is so obvious, I would just assume there are reasons it can't be done. And isn't your other suggestion just a repeat of item 1. In the question?
– Mark Ransom Apr 6 '10 at 22:51 1 Yes, my second suggestion is just a repeat of #1 - the idea was if he didn't like protected, my advice would be to prefer #1 over #2. – R Samuel Klatchko Apr 6 '10 at 23:00 I've currently got it implemented as #1. Thanks for reminding me to use a const reference.
– Will Apr 6 '10 at 23:11 I don't like the first option, push_back is much better from an encapsulation point of view. – Matthieu M. Apr 6 '10 at 7:24.
Another option is to create a (public or protected) constructor in base that will take the parameters and populate the vector directly. No intermediate vectors need be created or passed around. Class Base { .. .
Protected: Base(X* x,int n); .. . }; class Derived : public Base { Derived(X* xes, int n):Base(xes,n){}; }.
If you can, I would create a protected constructor in Base that accepts the X* argument just like the Derived constructor, and then in Derived's constructor initialization list, pass the argument to Base's protected constructor and have it do the actual insertion.
If you want the container in the base class to remain private, you might consider adding a public (or protected) function in Base to allow the derived class to add a range. This way, the Base container type can be encapsulated (Derived doesn't need to know it's a vector), and Derived can still pass the whole thing down in one shot: class Base { ... private: std::vector v; protected template void assign( iterator_t first, iterator_t last) { v. Assign( first, last); } }; class Derived : public Base { Derived(X* p, int n) { Base::assign( p, p+n); } }; It's not much of an encapsulation, but the container could be changed with a changing the derived class, and a derived class could easily be written that takes data in a form different than an array, if desired.
And as zdan suggested this functionality could be part of public or protected constructor for Base if that makes more sense (which is likely). The idea of using iterator ranges to represent the content or subset of a container is widespread in the STL.
You should set the member variable to be protected so that classes that inherit Base can access it. Alternatively keep the member variable private but add a protected accessor method to allow classes that inherit base to add to the vector etc.
No, that's a breach of encapsulation. Better to provide full fledged methods. – Matthieu M.
Apr 7 '10 at 7:23.
There is a third option if you can modify Base. Your base class can now directly operate on v. If you need to expose much of the vector functionality to Derived, this is the easiest way.
If not, I would just add the one function (i.e.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.