I have a class which looks like this :

class MyClass {
public:
    void drawText(const QString& rText);
    void drawText(const std::string& rText);
};

I overloaded the drawText() method because I want to accept QString as well as std::string.

But when I write something like this :

MyClass foo;
foo.drawText("Hello");

The compiler is complaining that the call to drawText() is ambiguous.

I understand that from an array of char, the compiler cannot decide between a QString or a std::string, because both provide a suitable constructor.

But is there a way for me to make sure the user can use the drawText() method either by passing a QString or a std::stringor an array of char ?

有帮助吗?

解决方案

To answer your question, yes: add another overload which takes const char*

The implicit conversion from const char* to QString is problematic because it assumes that the input is ASCII. I suspect the Qt folks would like to remove that constructor altogether but it would break source compatibility. If you want to disable it in your app, you can define QT_NO_CAST_FROM_ASCII.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top