Archive for the ‘Development’ category

C++ Standards Document

January 4th, 2011

It turns out that one has to pay for the official C++ standards document! Always thought that standards documents were available as free downloads. Anyway you can buy it from here:

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS/ISO/IEC+14882-2003

In any case at $30, it’s not all that expensive, considering that so far it’s only being updated once in a decade:)

Member operator vs Nonmember operator

May 23rd, 2010
Member operators vs nonmember operators
As I was recently reviewing some of the concepts of C++, this question popped up. So what is the difference between member operators and nonmember operators?
Well as it turns out there isn’t much of a difference as far as the language is concered, except the additional argument of the object type in the nonmember function implementation. Given an operation on a user-defined type, either the presence of a member or a nonmember operator would suffice to complete the operation. So then why does the language provide the option of choosing between the two?
The answer turned out to be simple.
A member operator is typically used when
- the operator modifies the value of the object to which it is applied, requring it access to the data type’s internal values.
- or the operator requires access to the object’s internal state, represented by data members which are not otherwise accessible (through a public method).
In all other instances a nonmember operator is to be preferred thereby ensuring that there’s less chance of the object’s data members getting iadvertently modified and also reducing the size of the object.
Examples for operators that explicitly modify the state of an object are +=, +-, <<=, etc. These operators are applied to an object an result of the operation is applied to the object to which the operator is applied.
Examples for the second case where member operators are necessary can be found in the standard library. The basic_ostream template class declares the ‘<<’ operator for all basic datatypes except ‘char’ (actually template parameter ‘Ch’) as member operators. This is because output of the charater representation of these datatypes depend on the various ostream state information as set by the user. Examples for these state include output number system (decimal, octal or hexadecimal), precision, width, display of ‘+’ sign, etc.
However, output of the ‘char’ does not require access to any state information. Consequently, ‘<<’ for various ‘char’ types (signed, unsigned, char*, etc) are achieved through nonmember functions such as:
template<class Ch, class Tr>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>&, Ch);
So the golden rule to learn is that if an operator can be implemented as a nonmember function, that is perhaps the best way to go as it would have the least impact on the object concered.

As I was recently reviewing some of the concepts of C++, this question popped up. So what is the difference between member operators and nonmember operators?

Well it turns out there isn’t much of a difference as far as the language is concerned (except the additional argument of the object type in the nonmember function implementation). Given an operation on a user-defined type, either the presence of a member or a nonmember operator would be enough. So then why does the language provide the option of choosing between the two?

The answer turned out to be simple.

A member operator is typically used when

  • the operator modifies the value of the object to which it is applied, requring it access to the object’s data members. Or,
  • the operator requires access to the object’s internal state, represented by data members which are not otherwise accessible (through a public method).

In all other instances a nonmember operator is to be preferred. This ensures that there’s that much less chance of the object’s data members getting inadvertently modified (which is only possible from a member method) and at the same time not increasing the size of the object. Refer to 11.3.1, C++ Programming Language, B.Stroustrup.

Examples for operators that explicitly modify the state of an object are +=, +-, <<=, etc. A real world code example:

class complex {
    double real, imag;
public:
    ...
    complex& operator+=(complex& c);
};

Examples for the second case where member operators are necessary can also be found in the standard library. The basic_ostream template class declares the '<<' operator for all basic datatypes except 'char' (actually template parameter 'Ch') as member operators. This is because output of the character representation of these datatypes (which is what the inserter operator accomplishes) depend on the various state information as set by the user. Examples for these state include output number system (decimal, octal or hexadecimal), precision, width, display of '+' sign, etc.
However, the output of  a 'char' does not require access to any state information. Consequently, '<<' for various 'char' types (signed, unsigned, char*, etc) are achieved through a nonmember function such as:


template<class Ch, class Tr>
       basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>&, Ch);

So the golden rule to learn is that if an operator can be implemented as a nonmember function, that is the best way to go as it would have the least impact on the object concerned.

Beta Qt SDK – Installation Issue

May 16th, 2010

I’m back to Qt again. This time prompted by my need for a simple application that will help me learn Chinese that I wanted to write for my cellphone. I have a Nokia N79 and I tried to venture into this adventure last year, but was put off by the S60 SDK and its flavor of ‘limited C++’. However, the announcement by Nokia of their plans to support Qt as the default application development framework for their mobile devices and plans to ship the runtime with the OS has re-kindled my interest in the framework.

Anyway, this is the issue. While installing the Beta Qt SDK, it fails to create the start menu shortcuts. The screenshot of the error is shown below.QtSDKInstallError1

This may very well be because of my peculiar environment. I’m running Vista on VMWare Fusion running on a Mac. For the first couple of shortcuts, I kept trying Retry to no avail. The Nokia Qt SDK –  beta release and the Symbian subfolder are created correctly though.

When I got the error again, I tried this. I created the shortcuts manually (same name as what is being displayed in the error message) from Windows and then tried the Retry option. And it worked just fine. I have posted a comment to a Nokia developer’s blog about this, let’s hope that it’ll get propagated to the right department.

Cross-platform Development – Qt

March 20th, 2010

Cross platform development has been a goal that I have held very close to my heart for quite some time. The notion of having an application or even a simple utility that can work across Windows, Linux and Mac somehow presents a charm that I find very appealing and something, try as I may, that keeps haunting my thoughts.

I tried my bit on wxWidgets, but found its MFC style architecture a little boring. I’m sure it works very well and there are many applications that use wxWidgets, but somehow wanted a framework that’s commercially supported while being a little bit more pure C++. (wxWidgets3 is supposed to be this, but it’s been coming for a very loong time).

I have even dabbled with XUL and XPCOM, but found the lack of a proper development tool very stifling. It’s amazing how many open source projects that have really cool technologies do not get their fair attention from other developers because of lack of proper tools. If only some of the Firefox team members forked out a project to create an integrated IDE that allowed designing the GUI and adding various event handlers from it. The whole concept of developing front end using an interpreted language such as Javascript is wonderful as it allows you to make quick changes to the presentation layer and get visual feedback immediately. In fact, this is better than the statically bound GUI such as those delivered by MFC, wxWidgets and such, but one needs a proper tool to create the initial content.

Anyway, having given up on the above two, I recently revisited another candidate that I had evaluated earlier — Qt. I had given up on Qt few years ago for their licensing costs were far too high. I felt that for a company trying to sell a new application framework, to get one better over the incumbents, they ought to price is really low. Anyway, with the recent acquisition by Nokia and their decision to have a GPL version that allows full proprietorship for applications built using it, I felt I ought to give it another shot. Also, their new tool, Qt Creator — the core IDE that allows everything to be done from one application — sounds very promising. Let’s hope it lives up to its perceived (purely mine) promise!

So over the next few weeks (or even months) I plan to spend a little bit of  time every week to look into Qt and how to create applications using it. I’ll try to document as much as I discover — both as a notes for my research as well as in the hope that someone else on the Net might find something useful in it. I can’t think of a specific application that I want to create, but I’m hoping that as I wander through the framework, I’ll have a ‘light-bulb’ moment leading to a worthy application to test the framework’s mettle.