Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Passing reference of class from call on class
Forum Updated to NodeBB v4.3 + New Features

Passing reference of class from call on class

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 3 Posters 948 Views 2 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I really like the way I can do this with a QString:

    QString message("blah %1 %2").arg(1).arg("what");
    

    So I wanted to emulate the passing of the original string object after modification. Basically after each call to arg return the original object reference. So in my own class I did this:

    class JMsg{
        QVector<QVariant> args;
        ...
        JMsg& arg(QVariant inarg){
            args.append(inarg);
            return *this;
        }
        ...
    };
    

    I want to pass the original object as a result of the call to arg(). Is it okay to pass *this as the source of the reference? It seems to work, but I want to avoid problems later if there is a better way to do this.

    C++ is a perfectly valid school of magic.

    kshegunovK JonBJ 2 Replies Last reply
    0
    • fcarneyF fcarney

      I really like the way I can do this with a QString:

      QString message("blah %1 %2").arg(1).arg("what");
      

      So I wanted to emulate the passing of the original string object after modification. Basically after each call to arg return the original object reference. So in my own class I did this:

      class JMsg{
          QVector<QVariant> args;
          ...
          JMsg& arg(QVariant inarg){
              args.append(inarg);
              return *this;
          }
          ...
      };
      

      I want to pass the original object as a result of the call to arg(). Is it okay to pass *this as the source of the reference? It seems to work, but I want to avoid problems later if there is a better way to do this.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @fcarney said in Passing reference of class from call on class:

      Is it okay to pass *this as the source of the reference?

      Yes. This is what you do in operator = for example. However this can make your code hard to follow, so keep that in mind.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      4
      • fcarneyF fcarney

        I really like the way I can do this with a QString:

        QString message("blah %1 %2").arg(1).arg("what");
        

        So I wanted to emulate the passing of the original string object after modification. Basically after each call to arg return the original object reference. So in my own class I did this:

        class JMsg{
            QVector<QVariant> args;
            ...
            JMsg& arg(QVariant inarg){
                args.append(inarg);
                return *this;
            }
            ...
        };
        

        I want to pass the original object as a result of the call to arg(). Is it okay to pass *this as the source of the reference? It seems to work, but I want to avoid problems later if there is a better way to do this.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @fcarney
        This is called Method Chaining. Common in jQuery and other JavaScript frameworks, less so in C++.

        1 Reply Last reply
        1
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          Here is some more info I found on this:
          https://en.wikipedia.org/wiki/Fluent_interface

          C++ is a perfectly valid school of magic.

          kshegunovK 1 Reply Last reply
          0
          • fcarneyF fcarney

            Here is some more info I found on this:
            https://en.wikipedia.org/wiki/Fluent_interface

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            I say it again, be careful with that stuff. You may mean well, but c++ is nasty; it isn't javascript (or even Java). If you decide to go this route for an API there are many, many pitfalls that you are going to need to observe. This also often leads to proliferation of classes to hold temporary expressions and such, you may be doing yourself a rotten favor.

            Read and abide by the Qt Code of Conduct

            fcarneyF 1 Reply Last reply
            3
            • kshegunovK kshegunov

              I say it again, be careful with that stuff. You may mean well, but c++ is nasty; it isn't javascript (or even Java). If you decide to go this route for an API there are many, many pitfalls that you are going to need to observe. This also often leads to proliferation of classes to hold temporary expressions and such, you may be doing yourself a rotten favor.

              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              @kshegunov
              Well the use case was to match the interface currently being used to provide variables to the tr() (translate functions). We have code that looks like this:

              tr("Log Message","Status: %1").arg(2)
              

              Well the problem with this is the string is in a server and the client needs to translate the message. So I wanted to wrap the message in a similar way and use the same arg() interface we currently use.

              JMsg(QT_TRANSLATE_NOOP("Log Message","Hello: %1 %2 %3")).arg(1).arg("full").arg("world");
              

              The JMsg overloads an operator:

              operator QString(){return toString();}
              

              This allows the call to be very similar to the original tr() call and return a string to the function it is created in. JMsg is intended to be a temporary variable. The string it produces is a JSON string that embeds the message with parameters and the args in a list. This is decoded by the client and translated using a function designed to interpret the JSON code.

              So effectively I am modeling it after how the translate parameter system works, for the API anyway. Not the underlying code.

              I don't program javascript or java much. Mainly C++ and Python.

              C++ is a perfectly valid school of magic.

              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