문제

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