Revising C++…
Loads of things have changed since 1998!!!
Loads of things have changed since 1998!!!
#include
#include
#include
class myClass {
public:
myClass(int);
~myClass();
int getI(){return i;}
private:
int i;
};
// constructor
myClass::myClass(int x) :
i(x) {
std::cout << "*** Calling: " << BOOST_CURRENT_FUNCTION << std::endl;
std::cout << "Contructor init: " << x << " --> " << this->i << std::endl;
}
myClass::~myClass(){
std::cout << "*** Calling: " << BOOST_CURRENT_FUNCTION << std::endl;
std::cout << "Bye... " << this->i<< std::endl;
}
int main() {
myClass my_class(10);
// dynamic
myClass* my_class_ptr = new myClass(20);
delete my_class_ptr;
// auto just compiles with flag: -std=c++0x
auto p1 = boost::make_shared<myClass>(1);
std::cout << "p1: " << p1->getI() << std::endl;
auto p2 = boost::shared_ptr<myClass>(new myClass(2));
std::cout << "p2: " << p2->getI() << std::endl;
boost::shared_ptr<myClass> p3 = boost::make_shared<myClass>(3);
std::cout << "p3: " << p3->getI() << std::endl;
const int n = static_cast<int>(p3->getI());
std::cout << "n: " << n << std::endl;
return 0;
}
The output:
*** Calling: myClass::myClass(int)
Contructor init: 10 --> 10
*** Calling: myClass::myClass(int)
Contructor init: 20 --> 20
*** Calling: myClass::~myClass()
Bye... 20
*** Calling: myClass::myClass(int)
Contructor init: 1 --> 1
p1: 1
*** Calling: myClass::myClass(int)
Contructor init: 2 --> 2
p2: 2
*** Calling: myClass::myClass(int)
Contructor init: 3 --> 3
p3: 3
n: 3
*** Calling: myClass::~myClass()
Bye... 3
*** Calling: myClass::~myClass()
Bye... 2
*** Calling: myClass::~myClass()
Bye... 1
*** Calling: myClass::~myClass()
Bye... 10