C++ implicit conversion: Why should literal string convert to string? [duplicate] - c++

This question already has answers here:
C++ implicit conversions
(4 answers)
Closed 7 years ago.
The example is from Chapter 7 in C++ Primer 5th.
Suppose class Sales_data has such constructor:
Sales_data(const std::string &s): bookNo(s) { }
And it has a public function member:
Sales_data &combine(Sales_data &s){...}
The flowing is error:(item is a Sales_data instance)
item.combine("9-999-9999");
The reason is that: Only One Class-Type Conversion Is Allowed, however, the code mentioned above has two user-defined conversions.
"9-999-9999" to string
string to Sales_data
Why should literal string convert to string ? Is 9-999-9999 not a string ?

"9-999-9999" is not a string, it is a const char[]. You can fix this by adding a constructor to you class that takes a const char * like:
Sales_data(const char* s): bookNo(s) { };
If you have C++14 support than you could also use a std::string_literal:
item.combine("9-999-9999"s);

Related

c++ the difference between string using the assign function and directly using '=' to change the value [duplicate]

This question already has answers here:
std::string::assign vs std::string::operator=
(2 answers)
Closed 2 years ago.
For example, this code
std::string a("this is a string");
std::string b;
b = a;
std::string c;
c.assign(a);
Is there any difference between B and C in essence?
From cppreference
2) basic_string& assign( const basic_string& str );
...
2) Replaces the contents with a copy of str. Equivalent to *this = str;. In particular, allocator propagation may take place. (since C++11)
So this does the same.

Why does my string get converted into a boolean when calling an overloaded function [duplicate]

This question already has answers here:
String literal matches bool overload instead of std::string
(4 answers)
Why is there an implicit type conversion from pointers to bool in C++?
(3 answers)
Closed 3 years ago.
I have an overloaded function called createValue:
void createValue(string, string);
void createValue(string, int);
void createValue(string, double);
void createValue(string, bool);
When I call the method obj->createValue("string name", "string value");, it calls the createValue(string, bool); overload and stores my string as a boolean.
I have changed that overloaded function to use char const* instead of just string and it works but I would like to know why this happened?

Overloading conversion operator from int to std::string [duplicate]

This question already has answers here:
Converting an int to std::string
(11 answers)
Closed 5 years ago.
Is it possible to overload conversion operator from int to std::string? Is it possible to use this function:
void printToLog(std::string text);
Like this:
int a = 5;
printToLog(a);
Or this:
int a = 5;
printToLog(a + " bombs have been planted.");
?
There's a function for it in the standard library. The aptly named std::to_string:
int a = 5;
printToLog(std::to_string(a) + " bombs have been planted.");
You cannot add a conversion operator for this purpose. Such an operator can only be a member of a class type, and int is not a class type. Neither can you add a converting constructor to std::string. Use the mechanism the standard gives you.

Initialize a c-string in C++11? [duplicate]

This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 7 years ago.
How do I initialize a c-string in C++11?
I tried:
char* foo = {"bar"};
but I get:
ISO C++11 does not allow conversion from string literal to 'char *
Because it has to be pointer to const:
const char* foo = "bar";
Before C++11, you could make foo simply char*, even though that was deprecated since C++03. But C++11 removes the deprecation and simply makes it illegal.
The correct way to do it is;
char const* foo = {"bar"};
// ^^^^^ added const
The older C style (non const) was deprecated and now is removed from C++11.

How to redefine = operator for strings in C++ [duplicate]

This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 7 years ago.
How can I perform this code with below details in C++:
mystring a;
a="A test text";
"mystring" is a class that is defined (by myself) for strings , and other operators like + , == , >> , << , etc are defined in this class.
How can i define a function (a friend function with class) that "=" perform something that I have mentioned.
if there were dictation mistakes, forgive me.
You need to define
mystring& operator=(const char*)
for this specific assignment to work.
Note that this overload returns a reference to self. This allows for compound assignments.
Not with a friend function. You need to overload the assignment operator :
mystring &operator = (mystring const &other) {
// ...
return *this;
}
Note that you'll also need a conversion constructor that takes in a C-string :
mystring(char const *str) {
// ...
}