Home arrow C and C+ arrow C++ Tips - Namespaces
C++ Tips - Namespaces PDF
Thursday, 19 March 2009

Coming from C world you might use the following code:

#include <iostream.h>

int main(void)
{
    cout << "First C++ program" << endl;
}




And you will get the following warning:

In file included from /usr/include/c++/4.2/backward/iostream.h:31,

                 from test.cc:1:

/usr/include/c++/4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

To remove warning and write a proper C++ code you must use one of the following:
 

a) Variant 1

#include <iostream>

int main(void)
{
    std::cout << "First C++ program" << std::endl;
}

 
b) Variant 2

#include <iostream>

int main(void)
{
  using std::cout;
  using std::endl;
  cout << "First C++ program" << endl;
}


c) Variant 3

#include <iostream>

int main(void)
{
    using namespace std;
    cout << "First C++ program" << endl;
}

 Best use is Variant 1, you use the namespace twice. If you use functions more often, Variant 2 is ok. Variant 3 is used if you many names from different namespaces. Still if you use Variant 3, do not use it globaly, but place it at the beggining of every function.

Last Updated ( Thursday, 19 March 2009 )
 
< Prev

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?