Tuesday, September 25, 2007

"I like to describe this as sitting between two stools. One foot in Rwanda. Another one in Canada," he says, smiling. "This is the beauty of Canadian multiculturalism. That you can jump into the salad bowl and remain a tomato."

-Lama Mugabo, balancing his perspectives as a Rwandan expat and as a Canadian Citizen.

Excerpt from Sept. 24, 2007, Vancouver edition of 24hrs. Article: small world, big problems - Rwanda.

Please visit Mugabo's NGO effort - Building Bridges with Rwanda - www.bbrwanda.org

Friday, September 22, 2006

Moving..

Dear Very-few-blog-readers-I-have,

Please find my future posts at http://kdjani.spaces.live.com

Cheers!!

Saturday, April 23, 2005

The Desi Connection

I just finished watching Kuch Kuch Hota Hai with my roommates. They are nice guys and ABCDs - more or less. It was like watching an India-Pakistan match sitting in the Pakistani section of the crowd. If my reaction upon seeing Kajol or Rani Murkherji on the screen would be, wow! Those guys would like. She is not hot. I want hot chicks! It is not that I do not fancy hot chicks. But for you at that time its just out of context. Its like talking about politics in a math class.

The connection is never there. They can never identify with me, my tastes. Nor can I identify with theirs. Even the sense of humor is different. You laugh with them because they find it funny. Not because you find it funny. The place where I spent 21 years of my life, and the connections and values established there, is what I am made up of. Its just different.

Its only a very few people you come across in life who can identify with you and you can identify with. With whom you keep going to the same restaurant over and over again and not get bored. With whom you can watch a movie - whether good or bad - and enjoy it. From whom we do not have the fear of being judged. These people whom we call friends and family.

Tuesday, April 19, 2005

Google-Craiglist mashup

Google and craiglist together. What do you think about that?

I first found about the google/craiglist mashup (http://paulrademacher.com/housing/) @ http://www.ypjain.com/anand. Also check out at http://code.google.com - our good friend google gives kudos to Paul for pulling this off.

I must admit, I am humbled. By Google and by this guy Paul.This is cool and intimidating. Google started off as a simple search engine. Today google has changed our perspective and ways in so many areas of mundane life. We search for information rather than seeking it( the old fashioned way). We search our mail rather than sorting it! We do not write in our own little diaries, we blog! and there are so many more examples. Internet was the one revolution of my lifetime. Google is going to be next.

We are always seeking Clarity, Knowledge, Awareness in life. What we currently have is a sum total of what we know(till now) and our perspectives about what we do not know. We live our lives equipped with this combination. Knowledge in one hand and perspectives in the other. If we knew, we would not have any perspectives! Because then we will just know.

Internet brings information together. Google makes it easier to find. End result. We become more aware.

Tuesday, April 05, 2005

Life surprises to remind that "the impossible" is possible

Monday, March 28, 2005

Assignment Operator - More C++ ramblings

Assignment Operator

Assignment operators perform the operation of assigning(copying) one object/entity to another EXISTING entity. In C++ assignment operator is available for built-in types and also for user-defined types. What makes them more interesting in C++ is that the users can overload/override assignment operators for user defined types.

The assignment operator is very similar to the Copy constructor in C++. The difference begin that operator= is invoked on existing(already created) objects. However copy constructor invoked at the time of object creation.

In the subsequent section we discuss the properties and caveats of the assignment operator. That will be followed with a short FAQS section.

Properties
Caveats
FAQs

Properties
Like all other language facilities, assignment operators have certain properties:

  • Compiler generates one if not specified

  • If the programmer does not specify an assignment operator the compiler automatically generates a default assignment operator for that class.
    Programmer can specify the assignment operator
    If the programmer provides a class level assignment operator, the compiler does not generate the assignment operator. This gives the programmer the freedom to modify the default operation of the assignment operator. However the programmer must ensure that semantics of the programmer defined assignment operator comply with the built-in types.

  • Shallow copy by default

  • The default compiler generated constructor uses shallow copy. It does memberwise copy. So if a particular member is a pointer variable the pointee is shared by both the objects as a result of the assignment. However the programmer can overload/redine the assignment operator to provide deep copy explicitly. The assignment operator and copy constructor should more less have the same behaviour.
  • Syntax of a member operator=

  • Let us try and declare the assignment operator for a String class

    class String
    {
    ....
    String& operator=(const String& rhs);
    ....
    };

    It is important not to deviate from the above mentioned declaration of operator=. For example:
    * If we define the operator= of type void. However this prevents cascading. The language garauntees the following facility for built-in types.

    class String { .... };
    String str1 = String("Hello World");
    String str2, str3;
    .....
    str3 = str2 = str1; // This will not be possible if the
    // return type is void

    * If we return the operator= by value or if we return a const reference it will allow the cascading but something silly like following will fail.

    .....
    (str3 = str2) = str1; // Something silly like this will fail. But the built -
    // in types support this type of an operator

    The above line of code is totally useless. But since built-in types allow such an operation, and your class does not support it the contract established by the language will be violated.

Caveats
The assignment operator in C++ is shipped with an army of caveats - ofcourse hidden :-).

  • Self Assignment

  • C++ allows something useless as this:

    ...
    String a;
    a = a;
    OR

    .....
    void foo (String& a, String& b)
    {
    ...
    a = b;
    ...
    }
    ...
    // In client code
    String str;
    foo(str, str);
    ...

    The above pieces of code illustrates self assignment. The operator= must address this case. If not, bad things can happen.

    Consider the following operator= for a String class not doing self assignment

    class String
    {
    ....
    public:
    String& operator=(const String& rhs);
    ....
    private:
    char* str;
    };
    .....
    // Bad assignment operator
    String& operator=(const String& rhs)
    {
    delete[] str;
    int len = strlen(rhs.str) + 1;
    str = new String[len]
    strcpy(str, rhs.str);
    }
    ...
    // In client code
    String string ;
    string = string; // Bad. The "string" pointed to by str
    // will be deleted first. And program
    // will crash when calling strlen
    ...

    Here is an example of a good assignment operator which addresses self-assignment.

    String& operator=(const String& rhs)
    {
    if ( this == &rhs)
    return *this;
    delete[] str;
    int len = strlen(rhs.str) + 1;
    str = new String[len]
    strcpy(str, rhs.str);
    return *this;
    }

  • Derived class operator=()

  • One of the very important things to realize when defining is a derived class operator= is that we are overloading and hiding the base class operator=.

    The derived class has a different scope from the base class scope. The derived class scope is more or less similar to the a nested class scope. Hence, any identifier( a function name or a variable name) re-defined/declared in the derived class, with same name as an identifier in the base class, will hide the identifier in the base class. Again this is primarily because, derived class has a DIFFERENT scope. NOT because the base class operator= is not inherited.

    All of the above story is important to us because every programmer must know that when providing a assignment operator for the derived class, the base class operator= must be explicity called since it is not called automatically unlike constructors. First lets see what happens if we do not do that:


    class Base
    {
    public:
    Base& operator=(const Base& rhs)
    {
    // This is more or less redundant but
    // doing it for consistency sake.
    if(this == &rhs)
    return *this;

    x=rhs.x;
    return *this;

    }
    private:
    int x;
    };

    class Derived : public Base
    {
    public:
    // Bad operator
    // The base class operator is not invoked automagically
    // Consequently, the base class part will not be copied
    // Only the derived part will be copied from rhs.
    Derived& operator=(const Derived& rhs)
    {
    // This is more or less redundant but
    // doing it for consistency sake.
    if(this == &rhs)
    return *this;

    y=rhs.y;
    return *this;
    }
    private:
    int y;
    }
    ...
    // Client code
    Derived d1( 3,4 );
    Derived d2 ( 6,7);
    d2 = d1;
    // This will print x = 6 ;; y = 4 !!!
    std::cout << d2 << std::endl;


    This can be easily fixed by the following code

    ....
    Derived& operator=(const Derived& rhs)
    {
    if(this == &rhs)
    return *this;
    Base::operator=(rhs);
    .......
    }
    ....


  • Member function or File-scope function

  • Make it a member function.
  • Object slicing

  • Consider the following piece of code

    class A {.... };
    ...
    class B: public A {....};
    // Client code
    .....
    A* a;
    B b;
    B c = B(blah1, blah2);
    a = &b;
    *a = c; // This is going to call A::operator=() unless
    // the operator= in A is virtual and overridden in B
    // Since the operator= is NOT virtual there is static binding
    // and the operator= of the static type (A in this case)
    // will be invoked.

  • Virtual Assignment operator

  • // Coming soon
  • Templates and assignment operator

  • // Coming soon
  • Old compilers and Default base class assignment operators

  • // Coming soon


FAQs
* Why should the return type of an assignment operator be Type&
// Coming soon
* When should I provide an assignment operator
// Coming soon

Tuesday, March 01, 2005

Reverse Gear

Just got some weird thought. The most unfair thing about life is the way it ends. I
mean, life is tough.

It takes up a lot of your time. What do you get at the end of it? A death. What's that, a bonus?!? I think the life cycle is all backwards. You should die first,
get it out of the way. Then you go live in an old age home. You get kicked out for being too healthy, go collect your pension, then, when you start work, you get a gold watch on your first day. You work forty years until you're young enough to enjoy your
retirement. You drink alcohol, you party, and you get ready for High School. You go to primary school, you become a kid, you play, you have no responsibilities, you become a little boy, you go back, you spend your last 9 months floating with luxuries like central heating, spa, room service on tap, then you finish off as an orgasm!! Amen"

- Musings with an old pal when he was at Stanford