Tuesday, January 27, 2009

On some useful MACROS in C/C++ for debugging purposes

Like a stupid idiot I begin all my C++ files with the following construct:

#include
static const std::string thisFileName = "foo.cpp" ;


and then within each function I declare the following

const string thisFuctionName = "void bar()" ;


I do this so that I can print some useful information when I write an error summary to the error stream.

if ( !paraFile.is_open() )
{
cerr << "File: " << thisFileName << endl ;
cerr << "Function: " << thisFunctionName << endl ;
cerr << "ERROR: Could not open "<< paraFileName << endl ;
exit(EXIT_FAILURE);
}


This way I don't have to individually change the filename and function
name keeping the general structure of the construct more or less fixed.

Today I found this useful presentation on this page which informed me that some
useful MACROS are predefined that take care of such situations.

__FILE__

and

__LINE__



I feel like a first class idiot not knowing these things.

Thursday, January 22, 2009

function overloading blues in C++

Can I overload functions with different return types ?

I wanted to overload a function just by changing the return type of the function. Turns out that this is not allowed at all :(
So the following overloading will not work

double squareMe(int x);
int squareMe(int x);

Got my answer here.

One solution is to do this :

void squareMe(double & y, int x);
void squareMe(int & y, int x);



Tuesday, January 6, 2009

Some make questions for C++ files

These questions are related to make when working with C++ files.
  • What is the difference between CPPFLAGS and CXXFLAGS ? What are some common values for these variables ?
    • In this a partial answer ?
  • LDLIBS and LOADLIBES ?

By the way to check the default variables that make uses, do the following:

make -p -f /dev/null


While looking for some answers to these questions I found this very helpful anchor.

C++ header files: angle brackets or double quotes ?

While looking for solution to a problem I stumbled onto this article. Discovered the reasons why C++ headers do not have a .h extension too.

That article also praises the book C++ in a Nutshell by Ray Lischner. I might have to check that one out though I learn't my C++ from Stephen Prata's excellent book C++ Primer.