Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
user defined exception
//clang 3.8.0 #include <string> #include <exception> #include <iostream> class MyException:public std::exception{ std::string m_error; public: MyException(std::string error):m_error(error){} virtual const char* what()const noexcept{ return m_error.c_str(); } }; template <class T> class Array4{ T *m_data=new T[4]; int m_index; public: T& operator[](int index){ m_index=index; if(index>=4) throw MyException("invalid memory reference"); return m_data[index]; } friend std::ostream& operator<<(std::ostream& out,Array4 arr ){ out<<arr[arr.m_index]; return out; } ~Array4(){delete[] m_data;} }; int main() { try{ Array4<std::string> arr; arr[4]="booom"; std::cout<<arr[4]; }catch(std::exception& e){ std::cerr<<"Error: "<<e.what(); }; }
run
|
edit
|
history
|
help
0
Dat pointers
mine
Error
move_string
MPL 2-0
GreedyProblem1
Example
Fun with Pointers #2
regex
A