1

I use Qt 5.2. In my program I want to change standard implementation of QString::toDouble(bool *ok) function. I write:

double QString::toDouble(bool *ok) const{
  return QLocale().toDouble(this);
}

When I compile it with option -std=C++0x it makes next error:

Multiple definition of `QString::toDouble(bool*) const'

I know, I can do it in languages like Ruby. I think this way is quite good to change toDouble locale. Please, correct me if it is a bad programming style or help to realize that.

Update But compiler allows me to change implementation of static function

QString QString::number(double n, char f, int prec){
    return QLocale().toString(n, f, prec);
}

Is it a bad programming style too?

Michael
  • 548
  • 8
  • 30
  • 2
    Without getting into the fact that this is bad style (it is!), I can tell you that it certainly is impossible in C++. – Magnus Hoff May 13 '14 at 11:11
  • Please, check new information in post. – Michael May 13 '14 at 11:50
  • You cannot alter functions of foreign classes withouth changing their sources. Subclass QString to be MyString and reimplement toDouble-method. As soon as you include original QString implementation you will again have 2 implementations with same footprints. – Sebastian Lange May 13 '14 at 14:21
  • @MagnusHoff Although it is in bad style in the vast majority of cases, there are some rare occasions where it isn't. `QString::toDouble` may be an external of which you don't have the source. You could also be receiving massive chunks of 3rd party source on a daily basis which use `QString::toDouble`, but you need to change `toDouble` for some reason. In this specific case, it could preferable to change `toDouble`'s implementation at runtime. Unfortunately C++ doesn't support this though. – OLL Jan 09 '17 at 11:59

2 Answers2

6

C++ (and C) don't support Ruby-style "monkey patching" because functions are bound together at compile time, not at run time. The rest of Qt has already linked against the old version of double QString::toDouble(bool).

(Objective-C, which uses dynamic dispatch for function calls much like Smalltalk and Ruby, actually does support this through "Method Swizzling".)

Depending on your OS, you may be able to take advantage of the loader to perform "Function Hooking". This is where the loader lets you re-implement parts of a shared library. There are a LOT of caveats, places this won't work, and ways to subtly cause hard-to-debug crashes. I only include these links for completeness; you should just take your code and make it into a new function:

Community
  • 1
  • 1
Tryke
  • 800
  • 1
  • 5
  • 8
  • But compiler allows me to change the static function. _Updated in post_ – Michael May 13 '14 at 11:40
  • 2
    Placing `static` at the beginning of a function definition has a different meaning. It means "make this function visible only to other code within this file." You may have inadvertently avoided a re-definition elsewhere, but it can't have changed the behavior of existing Qt library code -- that's already compiled. – Tryke May 13 '14 at 11:54
  • 1
    So compiler see my function and just doesn't looks for standard implementation. But he (compiler) doesn't guarantee me, that he will find my code first, right? – Michael May 13 '14 at 12:01
  • 'static QString QString::number(double, char, int)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] QString QString::number(double n, char f, int prec). As long as you stay in your class this would probably work, but any other call will probably use the original function. As Tryke mentions, this can lead to unpredictable behaviour ^ – Sebastian Lange May 13 '14 at 14:27
  • Thanx. It was such an easy solution. – Michael May 14 '14 at 04:50
3

It is not a bad programming style. It is simply an invalid construction in C++. You may not define the same (member) function twice. You could redefine a virtual function in a derived class but ut is not the same as to define the same function twice.

The other way is to define a derived class from QString class and inside it to define a function with the same signature that will hide the function in class QString.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    @Michael All member functions shall be declared inside the class definition. You may not to add member functions to a class outside its definition. – Vlad from Moscow May 13 '14 at 12:26