I used to use Turbo C++ compiler for c++. But now, I am using newer compilers such as code blocks,DevC++. This newer compilers need the this statement "using namespace std;". When I read this statement is used by the the c++ libraries(.h files) to define names. So Why do they define the names in the namespace? can't they define it in the .h file?
using namespace std?
Page 1 of 15 Replies - 630 Views - Last Post: 24 February 2011 - 04:50 AM
Replies To: using namespace std?
#2
Re: using namespace std?
Posted 23 February 2011 - 03:25 AM
1 - In modern C++ the standard libraries files do not end in '.h'.
That is C era naming which went out of date years ago. Because TuboC++ is so incredibly out of date you have been taught obsolete coding practice. Don't use '.h' at the end of your language includes. They are for non-standard includes only.
Look here for some new naming conventions
http://www.cplusplus.com/reference/
2 - I don't think you are really understanding what namespaces are and how they work.
Have a read here and if you have specific questions after reading that then ask away
http://www.cplusplus...ial/namespaces/
That is C era naming which went out of date years ago. Because TuboC++ is so incredibly out of date you have been taught obsolete coding practice. Don't use '.h' at the end of your language includes. They are for non-standard includes only.
Look here for some new naming conventions
http://www.cplusplus.com/reference/
2 - I don't think you are really understanding what namespaces are and how they work.
Have a read here and if you have specific questions after reading that then ask away
http://www.cplusplus...ial/namespaces/
#3
Re: using namespace std?
Posted 23 February 2011 - 03:42 AM
I also read the following text in a c++ book by Walter
"C++ comes with a number of standard libraries. These libraries place their definitions
in a namespace , which is simply a name given to a collection of definitions."
I think you didn't answer one question "Why do they place there definitions in a namespace??"
For instance the library iostream has its own .h file somewhere and so do all libraries. So why don't they place their
definitions in their own file.
Thanks for your cooperation.
#4
Re: using namespace std?
Posted 23 February 2011 - 03:49 AM
Tgafox, on 23 February 2011 - 08:42 PM, said:
I think you didn't answer one question "Why do they place there definitions in a namespace??"
Did you read the link above?
What about the information in that link do you not understand?
You need to ask specific, meaningful, questions to get good answers.
Your question doesn't make any real sense, as asked.
#5
Re: using namespace std?
Posted 23 February 2011 - 06:27 AM
The namespace is used so that the library can be split across multiple files, each of the different functionality of the standard library can then be included if and when they are needed. This means there isn't just one enormous standard library include header. Here's an example for clarity:
So lets say you have a section of code that only renders a box and doesn't need to know about collision
This is sometimes better than having to define a class to do these things. I hope that helps a little, even though my example was a little strange
// CollisionConvenience.h
namespace convenience
{
bool SphereSphereCollision(const Sphere& sphere1, const Sphere& sphere2);
bool BoxBoxCollision(const Box& box1, const Box& box2);
}
// DrawingConvenience.h
namespace convenience
{
void RenderSphere(const Sphere& sphere);
void RenderBox(const Box& box);
}
So lets say you have a section of code that only renders a box and doesn't need to know about collision
#include "DrawingConvenience.h"
void DrawScene()
{
convenience::RenderSphere(sphere);
convenience::RenderBox(box);
}
This is sometimes better than having to define a class to do these things. I hope that helps a little, even though my example was a little strange
#6
Re: using namespace std?
Posted 24 February 2011 - 04:50 AM
It helps... Excellent, that's the explanation I wanted...stayscrisp thanks
Janotte we don't need your claptrap links
This post has been edited by Tgafox: 24 February 2011 - 04:55 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|