Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use std::string vs char*? [duplicate]

Tags:

c++

std

Possible Duplicate:
C++ char* vs std::string

I'm new to C++ coming from C# but I really do like C++ much better.

I have an abstract class that defines two constant strings (not static). And I wondered if a const char* would be a better choice. I'm still getting the hang of the C++ standards, but I just figured that there really isn't any reason why I would need to use std::string in this particular case (no appending or editing the string, just writing to the console via printf).

Should I stick to std::string in every case?

like image 421
Noah Roth Avatar asked Jun 07 '12 18:06

Noah Roth


1 Answers

Should I stick to std::string in every case?

There are cases where std::string isn't needed and just a plain char const* will do. However you do get other functionality besides manipulation, you also get to do comparison with other strings and char arrays, and all the standard algorithms to operate on them.

I would say go with std::string by default (for members and variables), and then only change if you happen to see that is the cause of a performance drop (which it won't).

like image 57
K-ballo Avatar answered Sep 29 '22 23:09

K-ballo