1

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 ?

Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • 2
    You are using QT why not use only the QString and stay out of the std functionality? Combining 2 libraries with similar functionality is going to bite you in the ass later on. – RvdK Feb 10 '12 at 10:51
  • I'm usually using only QString and I could stick with it, but my question is more because of curiosity. – Jérôme Feb 10 '12 at 11:05

1 Answers1

2

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.

Dan Milburn
  • 5,600
  • 1
  • 25
  • 18