Wednesday, September 21, 2011

c++

After spending so many years programming in C#, I really appreciate how life make it so simple by language designers. Consider the following code written in c++:


Fraction a = b;

vs.

Fraction a;
a=b;

In C#, these 2 blocks of code are saying the same thing. It declares a variable and assigns it value b (i.e. a and b now pointing to the same object).

However, in C++, it is totally different. The first code block is to create an object of class Fraction by calling constructor(const & Fraction). The second block is to create an object with default constructor, and call the operator=(const Franction &). Depends on how you write your member functions of the class, the code might or might not work the way it looks.

I like C#'s version, since the code worked exactly it is read. Whereas, in C++, errors might be introduced.



dl

No comments: