A Developer's Diary

Mar 28, 2010

A const Reference and a Reference to const object

A reference serves as an alternative name or alias to an object. We cannot define reference to a reference type, but can make a reference to any other data type. C++ imposes several limitations on references. In particular, C++ does not allow you to change which memory a reference points to. Thus, all references are effectively 'const' references. Like normal constant objects, references must be given a value upon declaration.

What should we use then?

MyString& const ref = obj;
MyString const &ref = obj;

Read more ...

Mar 27, 2010

vimrc explained

VIM editor is one of the most popular text editor amongst the developers. At first look, it appears to be a daunting task to get used to it but once you get the hang of the unlimited functionality it offers at your hand, there is no going back and you will fell in love with the tool

A customized ~/.vimrc file

:syntax on        "Turn on syntax highlighting
:set laststatus=2 "Always show status line

:set autowrite    "Automatically write a file when leaving a modified buffer
:set confirm      "Start a dialog when a command fails (here when quit command fails)
:set tabstop=4    "Number of spaces a TAB in the text stands for

:set shiftwidth=4 "Number of spaces used for each step of (auto)indent
:set hlsearch     "Have vim highlight the target of a search
:set incsearch    "Do incremental searching

:set ruler        "Show the cursor position all the time
:set number       "Show line numbers
:set ignorecase   "Ignore case when searching

:set title        "Show info in the window title
:set titlestring=PANKAJ:\ %F   
    "Automatically set screen title

:set noautoindent
:set nosmartindent
:set nocindent

"Indent only if the file is of type cpp,c,java,sh,pl,php,asp
:au FileType cpp,c,java,sh,pl,php,asp  set autoindent
:au FileType cpp,c,java,sh,pl,php,asp  set smartindent
:au FileType cpp,c,java,sh,pl,php,asp  set cindent

"Wrapping long lines
:set wrapmargin=4   "Margin from the right in which to break a line. Set this value to 4 or 5

:set textwidth=70   "Line length above which to break a line

"Defining abbreviations
:ab #d #define
:ab #i #include

"Defining abbreviations to draw comments
:ab #b /********************************************************
:ab #e ********************************************************/
:ab #l /*------------------------------------------------------*/

"Converting tabs to spaces
:set expandtab

VIM Editor Tips

Navigating through VIM

Down (j), Up (k), left (h), right (l)
w (word forward), b (word backward)
W (word forward skip punctuation), B (word backward skip punctuation)
Page Down (Ctrl-f)
Page Up (Ctrl-b)
End of Line ($)
Start of Line (0)
Start of document (gg)
End of Document (G)
Jump to last place that you made an edit (g;)

Copy Pasting Contents

Surprisingly VIM inserts a lot of extra spaces when code is pasted from other applications. This can be corrected by executing the following in VIM mode

:set paste

Indentations can be corrected by running the following command in the system

indent -kr filename

Working with Visual Blocks

It is a feature that allows you to mark a block of text and perform editing operations on them. To enter visual blocks mode, press Ctrl+v, then use hjkl or arrow keys to highlight a block of text. Now operate on the first highlighted line as if you were in normal mode

Visual mode (v)
Visual line mode (V)
Visual block mode (Ctrl-v)

Converting tabs to spaces

Make sure you have :set expandtab set in your .vimrc file or otherwise execute set expandtab in normal mode and then run the following command to convert all tabs to spaces


Wrap a long line

Use (gq) to wrap the highlighted peice of text


Start typing the variable/function names and then (Ctrl-n) or (Ctrl-p) next and previous


delete character (x)
delete word (dw)
delete line (dd)


line (==)
entire document (gg=G)


%s/oldword/newword/g OR %s#oldword#newword#g
%s/oldword/newword/gc OR %s#oldword#newword#gc

Split Windows

Horizontal Windows :split
Vertical Windows :vs
Shifting between Windows (Ctrl-shift-ww)

Map Commands

If you find yourself re-typing the same command over and over, map it to one of the function keys as follows:

:map <fx> cmd

This maps the command cmd to function key Fx. For example, to map F5 to the command :!gcc -c % (compiles the current file) in normal mode

Code Folding

For large code blocks, you might want to fold/hide away certain functions temporarily. To fold a selection, select it in visual then (zf). To open a fold (zo)

Changing the Line Number Background and Foreground color

:highlight LineNr guibg=grey
:highlight LineNr guifg=blue
:highlight LineNr ctermfg=white ctermbg=grey

Read more ...