5 Replies - 896 Views - Last Post: 24 February 2011 - 04:50 AM Rate Topic: -----

#1 ashulala  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 11

using namespace std?

Posted 23 February 2011 - 02:38 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: using namespace std?

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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/
Was This Post Helpful? 0
  • +
  • -

#3 Tgafox  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 53
  • Joined: 23-February 11

Re: using namespace std?

Posted 23 February 2011 - 03:42 AM

:helpsmilie:
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. :whatsthat:

Was This Post Helpful? 1
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: using namespace std?

Posted 23 February 2011 - 03:49 AM

View PostTgafox, 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.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,195
  • Joined: 14-February 08

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:

// 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 :)
Was This Post Helpful? 2
  • +
  • -

#6 Tgafox  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 53
  • Joined: 23-February 11

Re: using namespace std?

Posted 24 February 2011 - 04:50 AM



It helps... Excellent, that's the explanation I wanted...stayscrisp thanks :bigsmile:

Janotte we don't need your claptrap links :dontgetit: , I needed just a simple explanation like stayscrisp did.

This post has been edited by Tgafox: 24 February 2011 - 04:55 AM

Was This Post Helpful? -2
  • +
  • -

Page 1 of 1