-
coding practices
Hi, I'm trying to migrate to C++ from C (which I had a basic knowledge of). I'm just wondering, what should I use:
Code:
using namespace std;
char x[50];
cout << "Hello\n";
cin >> x;
as an example, or:
Code:
std::cout << "hello\n";
std::string x;
std::cin >> x;
or something like that?
-
Well, you should definitely use the std::string type to hold sequences of characters, there's no doubt about it.
-
Rather the second. The first example is vulnerable to buffer overflows if user types more than 49 characters. The second is not. (Use smart containers over raw arrays, smart pointer over raw pointers).
You should probably not use using namespace in new code, but it's not that bad as long as you don't do that in a header.