开发者

Why doesn't c++ recognize a string when trying to convert it into another type?

开发者 https://www.devze.com 2023-04-04 03:47 出处:网络
I have a fairly simple class that looks like this: class Person { public: Person(string name): _name(name) {};

I have a fairly simple class that looks like this:

class Person {
    public:

        Person(string name): _name(name) {};

        void greet(const Person& person) const {
            cout << "Hello, " <<  person._name << "!" << endl;
        };

    private:
        string _name;
};

Note that the greet method takes a parameter of the Person type. When I pass it a Person object, it works as expected. Now let's pass it a string as a parameter in this way:

Person maher("maher");
maher.greet("sam");

When trying to run that code in QT (on a machine running ubuntu), it generates the following error: no matching function for call to ‘Person::greet(const char [4])’

I was able to resolve this error by casting the string in this way: maher.greet(string("sam"));

My question is the following: Why can't c++ 'see' that I'm passing a string to the greet method? Does it have anything to do with the fact that the greet method accepts a Person obje开发者_开发知识库ct?


maher is a const char[6], and sam is a const char[4], and both decay to const char * implicitly, but none of them is actually a std::string.

In function calls, the C++ standard allows an implicit conversion to be performed if there's a non-explicit constructor of the target type that accepts the type of the actual value passed to the function.

This is what happens when you call the constructor: you pass a const char[6], which automatically decays to a const char *; the target type is std::string, which has a constructor that accepts a const char *; such constructor is called, and the Person constructor correctly receives his std::string parameter.

In the second case, this is not happening: Person does not have a constructor that accepts a const char *, but only a constructor that accepts a std::string. To reach the desired Person type the compiler would have to first convert the const char * to std::string, and then call the Person constructor. This double conversion is not allowed, mainly because overloading resolution would become a complete mess (which already is) with lots of ambiguous cases.

If you want to allow greet to be called with a C-style string you should either:

  • create a constructor for Person which accept a C-style string (const char *), so that it can be constructed directly from a const char *, without going through the prohibited extra conversion

  • create another overload for greet to accept an std::string.

On the other hand, IMO the cleaner alternative is just leave it as it is; the caller will just have to write

maher.greet(std::string("sam"));


You aren't passing a std::string, you are passing a C-style string with type const char*. Add a constructor for that too:

Person(string name): _name(name) {};
Person(const char *name): _name(name) {};

Note that while const char* automatically can convert to std::string, in this case that would mean 2 conversions (const char * > std::string > Person), which is not allowed.


maher.greet("sam");

This requires two conversions:

  • First, const char[4] to std::string so that Person(string) can be called.
  • Then std::string to Person so that greet(const Person&) can be called.

But chain conversion is not allowed.

So you either provide a constructor Person(const char*), or pass std::string to avoid first conversion (listed above):

  • If you provide a constructor Person(const char*), then const char[4] will directly convert to Person which then will be passed to greet().
  • But if you pass std::string to greet(), then std::string will convert to Person using the existing constructor in your code, and then the person object will be passed to greet() eventually.

In both cases, there is only one conversion.

Or, simply write:

//convert const char[4] to std::string manually.
maher.greet(std::string("sam")); 


It absolutely does "see" that you're passing a string and that's the problem. You don't have a method that accepts a string as input.


Does it have anything to do with fact that the greet method accepts a Person object?
Yes,

You can pass a string to greet() method because there is a constructor in your Person class which constructs a Person object through the string that is passed to it.

Passing a char[4] requires the char[4] to a string object first ant then apply the above mentioned conversion. This chaining of conversions is not allowed and hence.


You could override the method to accept a string.

void greet(const string name) const {
    cout << "Hello, " <<  name << "!" << endl;
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号