Wednesday, June 20, 2007

printing combinations

Problem:
Print all combinations of M members of a set of N elements in a sequence such that each set can be obtained from the previous set by deleting one member and adding one member.

=================================
Take an example to understand:

given str: abcd , n =4, m =3
abc, abd, acd, dbc

main()
{
string s = "abcd";
sub = s.substr(0,m);
rem = s.substr(m+1, n);
comb(sub, rem);
}
comb( string s, string remain)
{
// extract the rest apart from first char
sub = s.substr(1, s.len);
// get all the combinations of the substr.
comb(sub, remain);
foreach ( char r in remain )
{
s[0] = r;
remain.remove(r);
comb(substr, r);
}
}


// take an example and run through the code to better understand the above.

Friday, May 18, 2007

Deleting a const pointer

Never thought that one can delete a const pointer in c++ without performing a const_cast on it in c++.

Tested this with the below code sample
========================
class A { public: int l; };
void test ( const A *aObj) { delete aObj; }
main() {
A *aObj = new A();
test(aObj);
}

=========================

To my surprise the code works on gcc-4.0.

So one cant be relaxed by just giving away a const pointer. Only way you can restrict the user from deleting is by giving out a reference or using smart pointers.

Even if they dont allow delete on a const pointer. The user can still invoke the desctructor explicitly by calling 'aObj->~A()' which is just another public function. So it seems like its a conscious decision made by c++ gurus.

==================
main() {
const A *aobj = new A();
// there should be a way for him to cleanup this..
// So makes sense to have delete supported.
delete aObj;
}
=================

snippets of some discussion forums :

"

However, this was always a little bit inconsistent,
since C++ has always permitted destroying an object via a pointer to
const by explicitly invoking the destructor, as in `p->~Foo()'. When
the committee decided to allow `const Foo *p = new const Foo', it
became pretty clear that they also had to allow `delete p'. So since
November 94, C++ has allowed deleting a pointer to a constant object. "

References:
http://www.thescripts.com/forum/thread439679.html
http://cpptips.hyperformix.com/cpptips/delconst.txt

Wednesday, May 9, 2007

Reflection programming model

I have been hearing a lot about the Reflection programming model.
The initial impression has been that how could a program modify itself and
where in the world does a software engineer need this ?

So here goes my hunt to demystify the myth behind this Reflection pattern.

Reflection is the ability to determine the behavior of your program at runtime.
Whats great? I can have many if conditions to get this done...
Yes you can change the behavior of the program with if clauses, but you cant dynamically determine what function to call without knowing the function name at compile time.

Reflection provides you a way to retrieve the name of the data members and member functions of a class and invoke them selectively at run time.
This is a read only reflection which is provided by Java.

Some languages (interpreters like PHP ) provides you an ability to add data members and member functions to a class at runtime.

In C a minimal set of reflection features can be obtained by dynamic libraries. A function can be looked up in the shared libraries using dlopen, dlsym and invoked.

The advantage Less code modification.


References :

1. http://en.wikipedia.org/wiki/Reflection_%28computer_science%29

For people familiar with php
http://php.corp.yahoo.com/manual/en/language.oop5.reflection.php