Posts

Showing posts from December, 2017

constructor throw exception

When a construct throws an exception then; that object is not fully constructed so destructor can not be called on that object. c++ destroys only fully constructed objects So this may leads to memory leakage. To solve this problem we have to 1. write a cleanup function which clears all allocated memory or resource 2. write the part of constructor block which  may leads to crash in try block & call cleanup function in catch block. #include<iostream> using namespace std; class first {     public:         first()         {             cout<<"in constructor of first"<<endl;         }         ~first()         {             cout<<"in destructor of first"<<endl;       ...

Why copy constructor is passed by reference not by pointer

The syntax of copy constructor says that it should be reference not pointer. 1. Passing by references ensures an actual object is passed to the copy constructor, while a pointer can have NULL value, and make the constructor fail. 2. if we pass a pointer then we have to dereference it. so it will be overhead for the user. Technically we can write a constructor which pass a pointer; but that is overloaded constructor not copy constructor.