Tuesday, August 7, 2007

Calling class Constructor C++

Some times we may think it would be nice , if we can call the constructor again for some initialization stuff (Normally constructor does the initialization job).  It is not possible to call the constructor of a class explicitly. Only way is to call is new operator. So here is that.

It is achived using placement new technique. it is very easy. For example if you have class CAlgoritham and the constructor of CAlgoritham does most of the initialization works. So after some processing you may want to  reset it to the old state (may be depnds on the algoritham). You have already wrote the initialization codes in constructor , but it is not possible to call constructor again. So we can use placement new. Here it is


CAlgoritham alg; //member variables Initialized already
alg.DoProces113();


new (&alg)CAlgoritham(); //this will call consructor again.But it  will not create a new memory (since we are giving memory location).


The statement new (&alg)CAlgoritham() is equal to CAlgoritham::CAlgoritham();

I like this kinda of things. Hopes you too. 

Saturday, August 4, 2007

STL Vector in VS 2005 ( when porting from 2003 or < )

In VS 2005 they changed almost all C runtime libraries and STL classes for adding security practices. Here is one information about the vector class in STL. So if you have any code written in 2003 , and if you port that to VS2005 be careful. 

In 2003 the following vector operations will work , like
vector<int> SomeInts;
SomeInte.push_back(0);
SomeInte.push_back(1);
SomeInte.push_back(2);


vector<int>::iterator it = SomeInts.Begin(); // Now it points to 0
++it;                                       // Now it points to 1
SomeInts.erase(it);                         // We are deleting the 1 from list
--it                                        // We want to move to 0 again.


and if you again ++it you will get 2. because we just deleted 1 from the vector. Ok, everything fine.
But In VS 2005 , the red marked line above(--it) cause to a crash!!! Open-mouthed. Because the iterator it is invalid so --it is also invalid.

 SO to correct that error best way is like this

vector<int> SomeInts;
SomeInte.push_back(0);
SomeInte.push_back(1);
SomeInte.push_back(2);
vector<int>::iterator it = SomeInts.Begin();   // Now it points to 0
++it;                                          // Now it points to 1
it = SomeInts.erase(it);                       // We are deleting the 1 from list
--it                                           // Now it points to 0


[ The last day , my friend told me he is getting a strange error in VS2005 , not in 2003, it was this problem. Smile]

Thursday, August 2, 2007

Calling Destructor & Virtual table c++

IF you really know how virtual functions work,it is really fun. Here is such an example(for fun i wrote a function fun  )

class base
{
public :
   virtual void fun()
   {
        cout <"base fun";
   }
  ~base(){}
};
class deriv: public base
{
    void fun()
    {
         cout << "deriv fun";
    }
   ~ deriv(){}
};

very simple two classes..
We know virtual funtions are initlaized in constructor and removed to their old base versions in the destructor.

Here is One more proof .
Try these if you are interested , otherwise go to www.youtube.com

base *p = new deiv();
p->fun();
p->~base();
p->fun();

the outputs will be first

deriv fun and
base fun

Because when we call p->~base() it will replaces the virtual pointer with the base version of fun. Thats why the base::fun get called.
We can't call constructor explicity as we did with destructor.
 Ok , I am stopping now.