Quote:
Suppose I want to use <cstring>
Forget <cstring>, it holds the Standard C Library functions for character array manipulation. By using
std::string, you can bypass that. The difference is akin to using
java.lang.String instead of manually allocating
char [] instances.
Quote:
How does that changes things like std::string naam?
Could I use just string naam?
In a header file, there is nothing you can really do. You can't use the technique I show below because it pollutes the global namespace.
In a source file, you can use a
using directive to tell the compiler where to find certain identifiers.
E.g:
// kandidate.h#ifndef KANDIDATE_H#define KANDIDATE_H#include <string>class Kandidate{public: // fully qualify type names in header files Kandidate(const std::string &name, int attribute); // ...private: std::string name; int attribute;};#endif// kandidate.cpp#include "kandidate.h"// tell the compiler that "string" means "std::string"using std::string;// now we can just use "string".Kandidate::Kandidate(const string &name, int attribute): name(name) attribute(attribute){}
However, once you tell the compiler you are using an indentifier (or an entire namespace, by saying using namespace <name>) you cannot undo this operation. This is why you should avoid putting such directives in header files, the client code (the code that includes the headers) have to live with your choices.
Modern languages don't have this problem. In Java, when you import class Bar in Foo.java, other files that happen to import Foo don't import Bar aswell. C++'s compilation model is ancient and a source of some of your confusion.
If you haven't already, I highly recommend you follow the link DevFred gave earlier.
Simple things like knowing how a C++ compiler considers each source file in total isolation will help you to understand what is going on.