0

Invoking an object with string as parameter,

eg: Check c1("Random");

to the constructor Check, there is a discrepancy based on the type, string vs char. While string needs the address operator &, char needs reference operator *. Why the difference ?

Explanations with examples would be appreciated, Thanks.

Check(const string &str = NULL)
{
    cout << str << endl;
}

vs

Check(const char *str = NULL)
{
    cout << str << endl;
}
Lastwish
  • 317
  • 10
  • 21
  • 5
    You're mixing up the "operators". First of all, when used in the context of declarations they are not *operators*, but a kind of modifiers (I don't know the correct term here). And the asterisk `*` is used to declare a *pointer* while the ampersand `&` is used to declare a *reference*. *Also*, a reference ***must*** reference something. You can not have a a reference to nothing (like you attempt to do in the first example). Perhaps you should take some time to [read a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Some programmer dude Nov 29 '17 at 07:27
  • `const string &str = NULL` is a bug. It's undefined behaviour to initialize a `std::string` with null pointer. – M.M Nov 29 '17 at 08:25

2 Answers2

2

i suggest you to look into the differences between string and char*.

Essentially, any continuous stream of characters is a string. C implemented them using char* - pointer to characters, AKA C-strings. These are null-terminated, such that the system knows when the end of string has been reached. C++ can also do that but it has its own string library which simplifies the usage compared to cstrings

Look into the link for in-depth explanation: https://www.prismnet.com/~mcmahon/Notes/strings.html

UPDATE: string by itself is a c++ object data type, whereas char* is simpy a pointer to a character (or continuous characters), which can be of variable length terminted by null characater \0

with implementation as such:

void printString(string& temp){
    cout << temp << endl;
}

Your function call would be like this:

string text = "lorem ipsum";
printString(text);

This function implementation expects to get a reference to a string type as its input parameter. Hence, when i declare a string variable text,i pass it on to the printString function by simply calling printString(text). The function call will take my input by reference. It does not create a copy of the temp string within the function call

If your implementation is:

void printString(char* temp){
    cout << temp << endl;
}

Then your function call would be:

char* text = "lorem ipsum";
printString(text);

This implementation takes a pointer as its input parameter and thus you have to pass a char* to the function. Unlike the previous method where you could simply pass your string type as-is and it would be passed by reference.

Both these function calls print out "lorem ipsum"

Suhaib Ahmad
  • 487
  • 6
  • 25
1

string is a class while char is a datatype. When a class object ex: string is passed by value to any function (including a constructor) a copy of the object is created on the stack. Now we have 2 objects containing the same data. If the constructor of this class does pretty heavy work then its a load to the program. Additionally copy constructor is not continuously for new unnamed object creation. Passing by reference doesn't create the object. It just calls a default/custom copy constructor defined in the class. char doesn't invoke any constructor so no additional work + same data is reference.