A Developer's Diary

Jan 31, 2011

Overloading operator()

Browsing through some of the open source code implementation in C++, came across the function void operator()()
Q. Why would one use operator() in the first place?
A. Simplicity of usage. Of course, less intuitive if you are seeing it's use for the very first time.

The program below implements a matrix and uses operator() as a replacement of subscript operator[] to retrieve and set values at the particular location.

#include <iostream>

//File: Matrix.h

class Matrix
{
public:
    Matrix(size_t rows, size_t cols);
    Matrix(const Matrix &m);
    virtual ~Matrix();

    Matrix& operator=(const Matrix &m);
    double& operator()(size_t row, size_t col);
    //double operator()(size_t row, size_t col) const;

    size_t getRows() const;
    size_t getCols() const;

private:
    Matrix();
    void copy(const Matrix &m);
    void init(double value);

    size_t _nrows, _ncols, _size;
    double *_data;
};

Excercise
Why is the method commented out in the above code?
//double operator()(size_t row, size_t col) const;

Read more ...

Jan 23, 2011

C Program - struct usage example

This is a follow up post on my last blog update Pointers in C - an eye opener. The program touches on the following -
1. Using struct as Employee record
2. Using pointer to Employee* as an employee list
3. Using void pointer and casting it to an appropriate type to avoid duplicate code
4. Allocating memory and freeing memory

struct Person
{
    char *name;
    size_t age;
};
typedef struct Person Employee;

struct Organization
{
    Employee **emplist;
};
typedef struct Organization Org;

A sample execution of the program should be as shown:


Read more ...

Jan 20, 2011

Pointers in C - an eye opener

My friend once asked me a problem in C involving struct and pointers. I was confident that the solution I had proposed would work untill I finally gave it a try.
Among the common mistakes i could not avoid were not allocating memory before trying to use the pointers and trying to dereference a pointer as p->q->*num :)

#include <stdio.h>

struct B
{
    char *str;
    int  *num;
};

struct A
{
    struct B *q;
};

int main()
{
    char *tstr = "Hello World";

    /**
     * 1. The program should copy the value tstr in str and print it
     * 2. The program should assign value 100 to num and print it 
     */
    struct A *p;
}

Read more ...

Aug 31, 2010

zthreads - building zthread dll using visual studio 2008

ZThreads is a cross platform object oriented library written in C++ which provides a clean and simple interface to both POSIX threads and Windows API threads.

Building ZThread shared library using Visual Studio 2008

1. Download ZThread source tar ball and extract it.
2. Create a new Win32Project using the Visual Studio installed templates
3. Go to Application Settings of Win32 Application Wizard
4. Select dll as the Application type and Empty project in the Additional options.
5. Add all the cxx files inside ZThread src folder into your project Source Files excluding the sub directories
6. Go to C/C++ tab in the Project Properties. Specify ZThread include directory in the Additional Include Directories
7. Change the default Character Set used by your Visual Studio project from Use Unicode Character Set to Not Set
8. Build the project

Read more ...

Aug 29, 2010

Mercurial - A short tutorial

Why Mercurial?

Mercurial can boast of really unique set of properties that make it a particular good choice as a distributed revision control system.
1. Easy to learn and safe to use
2. Lightweight
3. Delightful extensions and customization
4. Scalable
Download and install the Windows shell extension for Mercurial (TortoiseHg) from here

Creating a Repository

hg init main
where
main is the repository name

Mercurial reads configuration data from several files, if they exist. On Windows, it normally looks for %USERPROFILE%/mercurial.ini file or %USERPROFILE%/.hgrc file and in Unix based machines it looks for $HOME/.hgrc file. For detailed information type hg help config in the command line window or the terminal
Configuring Mercurial

Open %USERPROFILE%/mercurial.ini or %USERPROFILE%/.hgrc file and add following lines to it.

[ui]
username = Firstname Lastname <firstname.lastname@example.net>

This sets the username for the mercurial and this information is for reading by other users

Mercurial has inbuilt lightweight HTTP server that can be used to share the repository.
Serving the Repository

hg serve -p 80 -d
where
-p is used to specify a different port (default port 8000)
-d is used to run the server in the background

Creating a local copy of the Repository

hg clone http://host-name/
This creates a clone of the repository on the local machine

Read more ...