2

I am writing MyVector class which has std::vector as private field. And MyVector class should provide all the member types of std::vector. How can i solve this problem? Is a correct way to copy them like this(but it doesn't work):

class MyVector{
    private:
        vector<int> numbers;
    public:
        using value_type = vector<int>::value_type;
        using size_type = vector<int>::size_type;
        using iterator = vector<int>::iterator;
        using allocator_type = vector<int>::allocator_type;
        using difference_type = vector<int>::difference_type;
        using reference = vector<int>::reference;
        using const_reference = vector<int>::const_reference;
        using pointer = vector<int>::pointer;
        using const_pointer = vector<int>::const_pointer;
        using const_iterator = vector<int>::const_iterator;
        using reverse_iterator = vector<int>::reverse_iterator;
        using const_reverse_iterator = vector<int>::const_reverse_iterator;
};
ADV
  • 179
  • 9
  • You could inherit from it, though it is not a very good idea to do so. – Tanveer Badar Oct 09 '20 at 11:58
  • 2
    Why do you think this wouldn't work? – eerorika Oct 09 '20 at 12:04
  • 3
    What is the purpose of your class? What underlying problem is it supposed to solve? – Some programmer dude Oct 09 '20 at 12:07
  • I see you're not using `std::vector`, so you must have `using namespace std;` somewhere. Please read https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – AVH Oct 09 '20 at 12:08
  • 1
    Sounds like an [XY Problem](http://xyproblem.info/) – PaulMcKenzie Oct 09 '20 at 13:03
  • @Someprogrammerdude The main idea is copy-on-write idiom. I want to create custom class MyVector with private field std::vector and implement some methods and operators like push_back(), operator[], but with copy-on-write mechanics. Also i want provide access for member types like std::vector provides. – ADV Oct 09 '20 at 13:33
  • Sounds like you could use private inheritance for this. – Some programmer dude Oct 09 '20 at 14:29
  • @Someprogrammerdude But problem with virtual destructor still exists, am I right? – ADV Oct 09 '20 at 15:27

2 Answers2

1

There's no way to do this.

You can either inherit all the members of std::vector by publicly inheriting it and understanding how to cope with the lack of virtual destructor (thank you @M.A), or you can pick & choose as you are doing. There is no other way to specify which members to inherit.

But my personal advice is not to inherit std::vector. Better to be explicit about what you want to "inherit" and do as you're doing. Our goal should not be to save keystrokes.

tenfour
  • 36,141
  • 15
  • 83
  • 142
1

If you want to selectively use parts of the std::vector you can always use private inheritance and expose those functions you want in your derived public interface:

template<typename T>
class MyVector
: private std::vector<T>
{
private:
    using base_type = std::vector<T>;

public:
    using base_type::value_type;
    using base_type::iterator;
    using base_type::pointer;
    using base_type::reference;
    // ... etc ...

    using base_type::begin;
    using base_type::end;
    using base_type::operator[];
    // ... etc ...

};
Galik
  • 47,303
  • 4
  • 80
  • 117