Tuesday, October 27, 2009

callback and static member functions

you can not use non-static member functions as callback.because class member functions have a hidden argument i.e. this pointerpointing to the object.while for callbacks you need to have a function with exact declaration/signature.So you can't pass 'this' in the callback.On the other hand Static member functions do not have this pointer as they can be called without making an instance of class.So if you want to use a CLASS MEMBER FUNCTION for callback.It must be declared as static.Also static declaration is given inside class definition only,while define the function in cpp you don't give the keyword 'static'

static memeber not allowed inside C struct

Sumit does it again.
He gave me a programm which doesn't compile and asked the reason which I could not tell him,
have a look at the following snippet,
struct mystruct

{
float foo;
static int bar;//This line generates compilation error.
};

This is a perfectly valid c++ code,and bar can be accessed as mystruct::bar.
And it means that 'bar' will be shared accross all instances of mystruct.
But the scope resolution operator
is in C++ only,concept of name scoping as it exists in C++ was not there.
C is much more closer to machine.Also at the time of C being designed
It was never thought that they may need to share a var across different instances.
Also in C ,struct is not a user defined type instead it is just a collection of heterogenic
items.
So this code is not allowed in C.

Saurabh

Sunday, February 24, 2008

going back to C

Today sumit brought a programm,and i realized that so easy it is to forget the basics while you are busy with advanced programming.
here is the programm snippet,

float a=0.7;
if(a<0.7)
{
printf("\n a<0.7\n");
}
else
{
printf("\n nope\n");
}
Hewre what i thought that it'd print 'a<0.7'
but no because it is treated 0.7 as 0 only,
to make it work you have to explicitly cast
0.7 as (float)0.7.
or use another folat variable and store 0.7 in that.
Looks like should go thru the basics again:-)

Sunday, December 16, 2007

khichadi

Khichadi is an Indian recipe.It is a mixture of rice,pulses and spices.So anything which
includes ingredients from various classes can be called 'khichadi'.
This blog contains various technical titbits including but not limited to c/cpp programming tips,linux (general),linux(Ubuntu) installation/admin/programming tips.
So even after pondering over this i could not arrive to a name which could suite this blog.
Hence 'Khichadi', sorry 'TechKhichadi'.
Hope this proves to be useful.:-)