Thursday, November 01, 2007

Little Sugar : What does virtual in C++ mean

In c++ if a function is declared virtual for any class, then that class has an associated
virtual table . With each of those virtual functions a special type of virtual table pointer is associated , which has an entry in the virtual table associated with the corresponding class.

These virtual pointers are used at runtime to figure out which virtual function to invoke at runtime.

Making virtual functions inline does not make sense ?? why ...

Inline functions are meant to be made inline , so that means they don't have an explicit address but then if it does not have an address and its virtual then how will it have an entry in the virtual table ??

It will have an entry , cause our friend compiler will generate a function body for us and embed the address somewhere for it .

Tuesday, October 30, 2007

Private lock Object Idiom

The private lock object idiom is generally used in making a class Thread Safe.
Instead of using the instance of the object as a lock object an internal private object is used
as a lock object.

private Object lock = new Object();

so the method need to acquire a lock should use the lock object instead of this object.
e.g.

public void foo(){
synchronized(lock){
....
}
}