Monthly Archives: June 2011

Function Chaining in C++

Function chaining is a construct in  C++. It constructs a chain of function calls in one statement. Return value of the previous function call is used to call the next function and hence a chain of function calls is created. Following is an example of it:


obj.fun1() is an expression which evaluates to the return-value of func1(). On this returned-value func2() is called which again evaluates to its return value. Similarly func3().

Function chaining is good as well as bad. It may make the code look compact and nice but excessive use may end up with readability and maintenance issues. The code may be difficult to debug also. One can’t see the intermediate return values of the function calls.

Apart for the readability and debugging issues, it can also result into a serious memory issue. Lately, I faced it while converting from a QString to a char* C string. Here is the code:

// function QString::toLocal8Bit() returns QByteArray
// QByteArray::data() returns pointer to internal data

QString str("manjeet");
const char *c_str = str.toLocal8Bit().data();
// c_str is now a dangling pointer referring to an invalid memory

In the above code c_str points to an invalid memory.

It happens because the function chaining involves unnamed temporary objects. These unnamed temporary objects are returned by the function calls. And these are again used to call the next function call. But in this case the last temporary object is not stored and is deleted after the statement is complete.

This code can be fixed by avoiding the function chaining and storing the intermediate object.

QString str("manjeet");
QByteArray ba = str.toLocal8Bit();
const char *c_str =;

Here the problem is fixed as the byte array is now being stored the ba variable.



Control Mouse Speed from Command Line

I use two different mouse devices with my laptop, one in office and one at home. Mouse devices of different manufactures can have different sensitivity. This results in different speeds of the mouse pointer. To adjust the same, one can change the mouse speed from the Control Panel to achieve same speed or same experience with different mouse devices.

This has become a daily task for me. Twice a day, I have to change the pointer speed from the control panel. I thought of automating the process of changing the mouse speed to save myself from daily manual painful steps.

For part of the solution, I wrote a small utility program called changeMouseSensitivity.exe for changing the mouse speed. You can call this program from the command line passing mouse speed value (integer between 1 and 20) as the argument. This sets the mouse speed to the given value.

I calibrated both my mouse devices and found that the value of 6 for mouse_home and value of 14 for mouse_office gives me the same speed. I created two batch files mouse_home.bat and mouse_office.bat and in these two files I set the required speed values by calling the utility changeMouseSensitivity.exe. Following are the two bat files:

changeMouseSensitivity.exe 6
changeMouseSensitivity.exe 14

I created shortcuts to these two batch files on my desktop and every time I change my mouse (that is my location changes) I launch the respective bat file.

I’m liking this setup. It has reduced my total number of clicks from 7 to 1 double-click.

In future, there is a scope of automating the process completely. Complete automation requires detection of the connected mouse and setting the respective speed when mouse is connected.

Following is the source of the utility changeMouseSensitivity.exe. It is written in C++.

#include <stdio.h>
#include <tchar.h>
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
 if(argc == 2)
 int value = _tstoi(argv[1]);

 if(value > 20 || value <1)
 printf("\nFailure: the value should be between 1 and 20\n");
 return 0;

 BOOL result = SystemParametersInfo(SPI_SETMOUSESPEED, 0, (void*)value, 0);
 printf("  changeMouseSensitivity <interger value between 1 and 20>\n");

 return 0;

Use QRegExp Carefully

Lately, I found a silly mistake which I have repeated at a couple of places in my Qt code. I thought of noting it down.

Example Case

I have a QStringList (list) and I want to find the index of a string (str) in it.

// list     -  QStringList
// str      -  QString

int index = list.indexOf(QRegExp(str));
// this function accepts QRegExp only, though
// in the new releases of Qt an overload
// accepting QString is also available

Nothing wrong with this, it has been working great for me and I have used it at many places. But, this works as long as str doesn’t contain any RegExp special characters. If the str contains special characters, its meaning changes. For instance dealing with filePaths, filepaths may have special characters in it like the character +.

The meaning of QRegExp(str) changes if str has the character +:

  • str
  • QRegExp(str)

It will never match the original string (str). The index returned will be not be the valid one.


Escape all the RegExp special characters. On escaping the special characters, they will be treated normal and the code will behave correctly. Qt provides a utility function also.

QString QRegExp::escape(const QString&)

This function returns the escaped string (str).

  • str
  • escape(str)

So one can write the correct code like:

int index = list.indexOf(QRegExp(QRegExp::escape(str)));


In future, use QRegExp carefully if you just want to match a string.