0

I can not figure out how best to return values ​​for class members from getters.

QtCreator can generate getters automatically. For example, for a member of the class QString m_string, will be created the following function:

QString string() const;

Is it correct that a getter returns QString class member by value? Is it cheap? Would it be better if I write this:

const QString &string() const;

dtech
  • 47,916
  • 17
  • 112
  • 190
Shatur95
  • 93
  • 1
  • 9
  • 1
    Yes it is cheap, read http://doc.qt.io/qt-5/implicit-sharing.html – dtech Dec 31 '18 at 10:57
  • 1
    It depends. Returning `QString` makes a copy that is modifyable (which seems to be quite cheap as dtech pointed out). Returning `const QString&` is cheaper but does not allow modification like in `myobj.get_string().append("…")`. – Werner Henze Dec 31 '18 at 11:00
  • 1
    Nothing Qt specific at all. – πάντα ῥεῖ Dec 31 '18 at 11:03
  • @WernerHenze it is the opposite, you can't modify the property when returning by value because it will detach and modify the copy not the original, which wouldn't happen with a reference. However, that change will happen without emitting a signal which is considered erroneous behavior, plus I am not sure if QML will work with string references, likely not. – dtech Dec 31 '18 at 11:04
  • 2
    It IS Qt specific because Qt uses CoW, and and such this question is far more specific than the proposed duplicate. – dtech Dec 31 '18 at 11:05
  • @dtech Modern c++ string implementations also use COW meanwhile. Also I don't see how that's relevant here at all. – πάντα ῥεῖ Dec 31 '18 at 11:07
  • @dtech Returning `const QString&` of course does not allow modifying the returned string. Returning `QString` allows modifying the returned string. Of course you are only modifying the returned temporary, not the object member. – Werner Henze Dec 31 '18 at 11:07
  • @WernerHenze ah yeas I overlooked the const part. That would also apply to returning by value tho ;) – dtech Dec 31 '18 at 11:08
  • @πάνταῥεῖ there is also the notification component to Qt properties. – dtech Dec 31 '18 at 11:10
  • @WernerHenze, so, does returning by value have any sense at all? Does it make sense to change a temporary copy of the object? – Shatur95 Dec 31 '18 at 11:15
  • @Shatur95 That's all up to you, you can decide. Normally in a getter that accesses a sub object I return as `const&`. The construct `myobj.get_string().append("…")` has very limited use (`append` returns a reference to the temporary!). If you want it Qt style you could also check how Qt is implementing its getters. – Werner Henze Dec 31 '18 at 11:32
  • @WernerHenze, thanks for the explanation! Yes, in Qt, getters return by value. I think the creators of the framework know best. – Shatur95 Dec 31 '18 at 11:47
  • 3
    @πάνταῥεῖ I'm not sure whether `std::string` is modern in your book, but the standard [prohibits COW](https://stackoverflow.com/questions/12199710/legality-of-cow-stdstring-implementation-in-c11) for it. – Thomas Dec 31 '18 at 13:29

1 Answers1

2

Would it be better if I write this

"Better" is subjective. Have you established this to be a performance problem? Because generally, unwarranted micro-optimizations are considered a bad programming practice.

CoW has a tiny overhead, but unless you establish that it creates a problem for you, it shouldn't be a concern. If your proposed method was better, I am sure Qt would use that format to auto-generate getters, and also propose it in the documentation.

This doesn't mean that your format won't be objectively more efficient in your specific narrow use case scenario. But it does mean the returns you will get may not be worth the extra key presses to implement manual getters, although you could use a macro for properties, like most people do.

The format Qt uses is more universal, for example your format might be problematic if you want to use that property from QML, which may not support string references, and doesn't delegate const correctness from C++ yet.

Also, returning a mutable reference opens the door to modify object properties without going through the correct code path that would also notify of the change, which may have adverse side effects if your code is monitoring and reacting to data changes. I presume you are referring to properties here, since you talk about auto-generated getters, not about plain class members.

dtech
  • 47,916
  • 17
  • 112
  • 190