Retourner au contenu associé (dépêche : Naissance d'un géant : Java)
Posté par errno le 12 juillet 2011 à 09:50. 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
$
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
[^] # works for me
Posté par errno . En réponse à la dépêche Naissance d'un géant : Java. Évalué à 2.
$ cat vector_inherits.cpp$ g++ -o vector_inherits -Wall vector_inherits.cpp$ ./vector_inheritsreceived exception$