• [^] # works for me

    Posté par . En réponse à la dépêche Naissance d'un géant : Java. Évalué à 2.

    $ cat vector_inherits.cpp

    #include <vector>
    #include <iostream>
    #include <exception>
    template <class T, class A = std::allocator<T> >
    class MyVector : public std::vector<T,A>
    {
    public:
     const T & operator[](typename std::vector<T,A>::size_type i) const {
     if (i >= std::vector<T,A>::size()) throw std::exception();
     return std::vector<T,A>::operator[](i);
     }
    };
    int main()
    {
     MyVector<int> v;
     v.push_back(5);
     try {
     std::cout << v[1] << std::endl;
     } catch (const std::exception & e) {
     std::cout << "received exception" << std::endl;
     return 1;
     }
     return 0;
    }
    

    $ g++ -o vector_inherits -Wall vector_inherits.cpp
    $ ./vector_inherits
    received exception
    $