Using namespace in header file in c++What are the pros & Cos of using in header file
17 Replies - 3204 Views - Last Post: 08 March 2010 - 12:12 PM
#1
Using namespace in header file in c++
Posted 04 February 2010 - 10:55 AM
It is obvious that "using namespace" should be added in the cpp file not in the header file.
But i want to know what are the pros & cos using this in header file.
Subhransu Panigrahi :)
Replies To: Using namespace in header file in c++
#2
Re: Using namespace in header file in c++
Posted 04 February 2010 - 11:00 AM
! if you add 'use namespace ___' in a header file, you don't need to add it to the .cpp file that includes the header. It will be a duplicate, and therefore, efficiency drops.
#3
Re: Using namespace in header file in c++
Posted 04 February 2010 - 10:47 PM
#4
Re: Using namespace in header file in c++
Posted 04 February 2010 - 11:04 PM
Quote
You must have a very, very, very slow machine!
#5
Re: Using namespace in header file in c++
Posted 04 February 2010 - 11:19 PM
subhransu.panigrahi@gmail.com, on 04 February 2010 - 11:55 AM, said:
It's often that people don't even post the correct headers, they swap between C & C++. I have no idea what putting namespace into a header & using it on a C project would do. I also don't care to find out
But my point is, that beginners sometimes have a hard time understanding proper header usage, let alone customization.
#6
Re: Using namespace in header file in c++
Posted 05 February 2010 - 12:10 AM
Quote
The compiler will be able to handle it. C++ is a superset of C, and as such cannot distinguish between the two. If you tell the compiler the progam being compiled is C only, then it effectively switches of all C++ constructs and statements.
#7
Re: Using namespace in header file in c++
Posted 05 February 2010 - 01:02 AM
#8
Re: Using namespace in header file in c++
Posted 05 February 2010 - 01:41 AM
#9
Re: Using namespace in header file in c++
Posted 05 February 2010 - 02:06 AM
-->Are you saying every c++ compiler compiles c?
Ans: NO.
Reason let say in C there is a function which converts void function to other one.But the same will not work as C++ will not allow.
http://www.cprogramm...l/c-vs-c++.html
#10
Re: Using namespace in header file in c++
Posted 05 February 2010 - 03:05 AM
aFunction() {
Will not compile. In C it will default to returning int; C++ does not allow this.
Also, as mentioned, there is much stricter typing. Calls like malloc() and GlobalAlloc() must have explicit typecasting in C++, but not C:
int *myint = malloc(sizeof(int) * 2); //Doesn't work in C++ int *myint = (int*)malloc(sizeof(int) * 2); //Works in C++
#11
Re: Using namespace in header file in c++
Posted 05 February 2010 - 03:25 AM
Quote
I never write lazy C, so I have never come across these compiler descrepancies. ISO standards state quite specifically "C++ is a superset of C". Agreed, compiler manufacturers implement this in odd ways giving rise to lazy code.
#12
Re: Using namespace in header file in c++
Posted 05 February 2010 - 08:03 AM
seeP+, on 05 February 2010 - 03:02 AM, said:
Most C++ compilers will compiler a file as C if you name it .c or use a command line switch to that effect. I think the GNU compilers actually seperate into two differnt commands but most of the other compilers I am aware of have a "c mode". For example Visual Studio/Borland will both compile a program as ANSI-C if the file is a .c file.
This little program:
testtest.c
#include <iostream>
int main() {
std::cout << "hello world" << std::endl;
return 0;
}
Borland C++ 5.5: Failed
VS 2010: Failed (apparently iostream include cmath... didn't know that)
MinGW (g + +): success -- "the exception that proves the rule"
DigitalMars: Failed
So those three all have a C mode... and mingw/gnu separate things out to two different compilers.
Though note that if you go the other way and take a C program and name it .cpp than most of the time a C++ compiler will compile it with no problems or errors.
There are some *slight* differences between C and C++ (like some operator precedence differences that randomly pop up) but for the most part any modern C program will compile in a modern C++ compiler.
#13
Re: Using namespace in header file in c++
Posted 05 February 2010 - 08:14 AM
PlasticineGuy, on 05 February 2010 - 05:05 AM, said:
aFunction() {
Will not compile. In C it will default to returning int; C++ does not allow this.
Also, as mentioned, there is much stricter typing. Calls like malloc() and GlobalAlloc() must have explicit typecasting in C++, but not C:
int *myint = malloc(sizeof(int) * 2); //Doesn't work in C++ int *myint = (int*)malloc(sizeof(int) * 2); //Works in C++
You are correct there. C++ is much more type safe than C and requires that you obey a few rules to explicitly state type conversions. I kind of forget about that since I don't really write C very often (and when I do, I don't really write "lazy C" -- though I don't typecase malloc -- I seem to remember that there was a reason why but I don't recall).
#14
Re: Using namespace in header file in c++
Posted 05 February 2010 - 09:31 AM
PlasticineGuy, on 05 February 2010 - 09:05 AM, said:
aFunction() {
Will not compile. In C it will default to returning int; C++ does not allow this.
Also, as mentioned, there is much stricter typing. Calls like malloc() and GlobalAlloc() must have explicit typecasting in C++, but not C:
int *myint = malloc(sizeof(int) * 2); //Doesn't work in C++ int *myint = (int*)malloc(sizeof(int) * 2); //Works in C++
you should avoid malloc() and calloc() commands in c++ anyways. The correct way is to use
new
#15
Re: Using namespace in header file in c++
Posted 05 February 2010 - 09:35 AM
There are times when one must integrate with C libraries/functions.
I think the point was compiling C code (which can not use "new") with a C++ compiler has some differences such as C++'s type-pickyness vs C's free love attitude.
BTW -- the C standard library functions are part of the C++ standard -- so using malloc() or printf() or gets() in a C++ program may be *bad* but it is still standards compliant. It is still perfectly valid C++.
|
|

New Topic/Question
Reply



MultiQuote







|