Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Overloaded methods can't call each other on QString &value
Forum Updated to NodeBB v4.3 + New Features

[Solved] Overloaded methods can't call each other on QString &value

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StephanWoebbeking
    wrote on last edited by
    #1

    Hi, I was trying to write two overloaded functions as they are doing pretty much the same, but I need the result from the one where I am not interested in by the other:

    @
    bool getRawParameter(byte *data, int len);
    bool getRawParameter(byte *data, int len, QString &returnValue, int retLen);
    @

    Now as I can use the second from the first whilst just throwing away the returnValue, I thought of implementing like this:

    @
    bool MainWindow::getRawParameter( byte *data, int len) {
    return getRawParameter( data, len, NULL, 0 );
    }
    @

    But Qt complains of not being able to convert from 0 to QString &. Using "QString()" as parameter results in not being able to convert from QString to QString&. I have tried a couble of other things, the only solution so far I found is to create a QString within this method and pass that variable without doing anything with it after the call.

    I that really the only solution to it? I quite like the mechanisms of overloading and use it a lot, would you say that there is a fundamental problem in the structure I have outlined? Any other suggestions?

    Thanks a lot,
    Stephan

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QString& is a modifiable reference so "in-out" type. i.e you should only use it if you're going to pass something out from the function.
      If that's really the case then you should do something with that result value so you can't pass a temporary QString(), as the value would be assigned to a temporary and destroyed, which would be weird. This is illegal because this is usually not intended and a bug. If it's the rare case you really mean it then yes, you need to create a separate QString variable and pass it in.

      If the parameter is intended to be just "in", then the type should be const QString& in which case you can use QString() to pass a default value in, as the compiler knows the temporary is not going to be assigned to.

      In any case QString& is not a pointer type so you can't pass NULL there.
      NULL is just a macro defined as "0", so what you do there is trying to convert integer 0 to QString, which is nonsense of course.

      NULL is also discouraged these days and if you mean a null pointer then there is a properly typed value for that now: nullptr. Prefer it instead of NULL or 0. nullptr is a pointer type so it will obviously not convert to QString& either.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        StephanWoebbeking
        wrote on last edited by
        #3

        Hi Chris,

        thanks for the quick reply! It already helped in so far I understand that I have to "actively" throw away the result.

        I was actually looking for a solution where I can decide on the parameter if the caller is interested in the result like this (I did it in Java a lot...):

        @
        void method( String val ) {
        if ( val != null ) {
        // do some processing
        val = ...;
        }
        }
        @

        Then I could call it like this:
        @
        String val;
        method( val );
        @

        Or, in order to only benefit from the internal processing, like this:
        @
        method( null );
        @

        So I suppose this is not really possible in Qt, correct?

        Thanks,
        Stephan

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It has nothing to do with Qt, it's just c++ and yes, it's quite possible. Just use a pointer instead of reference:

          @
          void method( QString* val = nullptr ) {
          if ( val != nullptr ) {
          // do some processing
          *val = ...;
          }
          }

          //then call it:
          QString s;
          method(); //does nothing
          method(nullptr); //does nothing explicitly
          method(&s); // puts stuff into s
          @

          1 Reply Last reply
          0
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            But to be honest this design is not very good because the responsibility separation is poor.
            Instead of this
            @

            void method(Type* param) {
            //do some stuff
            if(param)
            *param = //put stuff into param
            }

            //call
            Type p;
            method();
            method(&p);
            @
            I would go with this:
            @
            void doStuff() {
            //do stuff
            }
            Type getSomeStuff() {
            //call doStuff() here if it's always required
            return //some stuff
            }

            //call
            doStuff();
            Type p = getSomeStuff();
            @
            The responsibility division is much cleaner. It's easier to call just the "doStuff" part without worying about what side effects a call could have and (on the low level side) you're avoiding the check entirely.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              StephanWoebbeking
              wrote on last edited by
              #6

              Thanks for the initiative, I have changed it and use a mixture of overloading and your suggestion, looks quite good. :o)

              CU,
              Stephan

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved