3

In C++, if string is a class, why do we not need the dot operator or an object to store data in a string?

Classic string:

string str = "ABC";

Why can we directly pass ABC using " " instead of doing it like

string str;

str.data = "ABC";

But we need to use objects to access the functions. Example:

str.length();

Why do we do this?

Is string some special kind of class?

CodeSadhu
  • 376
  • 2
  • 4
  • 15
  • In this case it's a constructor. But C++ allows you to overload operators which can make it look you are manipulating the object without methods, but this is just syntactic sugar to call methods on the object. – Martin York Apr 12 '19 at 05:27
  • 1
    `std::string` is not a special kind of class, it's a very ordinary kind of class. I suspect that you're familiar with some other language and would benefit from [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 12 '19 at 06:12

3 Answers3

3

string str = "ABC"; is not assignment. It is construction. Specifically it calls the std::string constructor taking a const char * argument.

It's the same as doing

string str("ABC");

just different syntax.

Assignment also works. You do this:

string str;
str = "ABC";

See also:

Copy initialization

std::string constructors

std::basic_string::operator=

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
2

std::basic_string has a constructor like this:

basic_string( const CharT* s, const Allocator& alloc = Allocator() );

Constructs the string with the contents initialized with a copy of the null-terminated character string pointed to by s.

But the important point to note is that this constructor is not explicit, thus the compiler can do implicit conversion of null terminated character string during constructor call.

For example, following code compiles without any issue:

class Foo {
public:
    Foo(int) {}
};

int main() {
    Foo f = 10;
}

It won't compile if the constructor is written as:

explicit Foo(int) {}

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • So if the string value is the const char argument and the second argument is mostly NULL, what do I need to assign to initialize the second argument? I mean to ask what (if necessary) to be passed as the const Allocator? – CodeSadhu Apr 12 '19 at 05:27
  • Seconds argument is not null, it is the default one. All standard containers are allocator aware containers, that is they give us option to pass custom allocator if the default one is not suitable for us. You don't have to think about allocator parameter unless you are doing really advanced works. – taskinoor Apr 12 '19 at 05:30
0

In C++ a string literal is not a std::string, but a C style character array(char[N]). And yes std::string or any other 3rd party string type that you may see is a class with a converting constructor accepting character arrays as input. More precisely, std::string is a type alias for an instansiation of the template std::basic_string. In short words, before you can do anything with a string literal, you'd better convert it to a string:

std::string{"ABC"}.size()

Or you will have to switch to C API which is not recommended for beginners:

strlen( "ABC")
Red.Wave
  • 2,790
  • 11
  • 17