Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C++ object lifecycle example
#include <iostream> #include <stdexcept> #include <sstream> #include <memory> #include <typeinfo> #include <limits> using namespace std; class FooEntry { int m_x; public: FooEntry(int x) { cout<<"Called "<<typeid(*this).name()<<" ctor."<<endl; if(x <= 0) { stringstream errMsg; errMsg<<"Invalid argument: "<<x<<"."<<endl; throw invalid_argument(errMsg.str()); } m_x = x; } ~FooEntry() { cout<<"Called "<<typeid(*this).name()<<" dtor."<<endl; } inline int GetFoo() const {return m_x;} }; class BarEntry { auto_ptr<unsigned char> m_p; public: BarEntry(size_t n) //: m_p(new unsigned char [n]) { cout<<"Called "<<typeid(*this).name()<<" ctor."<<endl; m_p.reset(new unsigned char [n]); } ~BarEntry() { cout<<"Called "<<typeid(*this).name()<<" dtor."<<endl; } void SetBar(unsigned char bt) { m_p.get()[0] = bt; } unsigned char GetBar() const {return m_p.get()[0];} }; class Holder { FooEntry m_foo; BarEntry m_bar; public: Holder() : m_foo(1), m_bar(numeric_limits<int>::max()) { cout<<"Called "<<typeid(*this).name()<<" ctor."<<endl; } ~Holder() { cout<<"Called "<<typeid(*this).name()<<" dtor."<<endl; } void Perform() { m_bar.SetBar(m_foo.GetFoo()); cout<<"Bar entry is "<<m_bar.GetBar()<<"."<<endl; } }; int main(int argc, char* argv[]) { try { Holder h; h.Perform(); } catch (exception& e) { cerr<<e.what()<<endl; } catch (...) { cerr<<"Unknown error."<<endl; } cin.get(); }
run
|
edit
|
history
|
help
0
GET ALL PRIME FACTORS OF A NUMBER
typename T class T
remove dublicates from string using recursion
infix to postfix v 6.0 - (exponent support + multiple digits)
project
Cpp update 1
C++
Queue with Limited Size of Arrays
OverloadFunc
12535