Visual C++ Preprocessor null directive

30 03 2008

Icon Description
Well, add an empty # symbol to your #include list and compile the project. For e.g. see the following #include list.

#include “stdafx.h”
#include “Dialog.h”

#
#include “DialogDlg.h”

It will compile without showing any errors. Weird?

icon_underthehood.jpg
The single “#” in a line is called null directive. It has no effect. Indeed, its not a bug. 😉
But i can’t find the answer for question… What is the purpose?

Advertisements




Reduce iostream compilation dependency by using iosfwd

30 03 2008

Icon Description
For making our classes compatible with cin and cout, we usually over load << and >> operators. And if such classes are the part of our modules interface, the iostream header file should be added to the interface header of our module. It makes compilation overhead for the classes those use our exported class. Please see the following code block.

#include <iostream>

class IMyInterface
{
public:
    // Overloaded << operator.
    std::ostream& operator<<( std::ostream& os );
};

Icon How Can I Do It?
You can easily reduce the compile time dependency of iostream header by using iosfwd. It contains forward declaration of several template classes defined in iostream. Please check MSDN for more details. Don’t forget to include iostream in your cpp files, since iosfwd contains just forward declarations.

#include <iosfwd>

class IMyInterface
{
public:
    // Overloaded << operator.
    std::ostream& operator<<( std::ostream& os );
};