Planned maintenance: From Sunday 8th December 10:00 CET there will be changes to try and solve the caching issues that have been experienced. If anyone has a problem connecting after this period then please PM @AndyS or any of the moderators.

Passing reference of class from call on class



  • 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.


  • Qt Champions 2017

    @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.



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



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


  • Qt Champions 2017

    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.



  • @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.


Log in to reply