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. QDataStream default in function argument
Forum Updated to NodeBB v4.3 + New Features

QDataStream default in function argument

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k 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.
  • FranckynosF Offline
    FranckynosF Offline
    Franckynos
    wrote on last edited by
    #1

    Hi everybody,

    I need a function like

    void foo(QString cmd, QDataStream s);
    

    But the compilateur say (const QDataStream&) is private
    Q_DISABLE_COPY

    So i can do it with reference like

    void foo(QString cmd, QDataStream &s);
    

    And it works. But i would like the second parameter as optional like this

    void foo(QString cmd, QDataStream &s = QDataStream());
    

    But it doesn't work, i don't know the syntax.

    Someone can help me ?

    Thx

    K 1 Reply Last reply
    0
    • FranckynosF Franckynos

      Hi everybody,

      I need a function like

      void foo(QString cmd, QDataStream s);
      

      But the compilateur say (const QDataStream&) is private
      Q_DISABLE_COPY

      So i can do it with reference like

      void foo(QString cmd, QDataStream &s);
      

      And it works. But i would like the second parameter as optional like this

      void foo(QString cmd, QDataStream &s = QDataStream());
      

      But it doesn't work, i don't know the syntax.

      Someone can help me ?

      Thx

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Franckynos

      Try:

      void foo(QString cmd, const QDataStream &s = QDataStream());
      

      You are using a reference to QDataStream, therefore, you need to declare it as constant. It will make no sense to assign in the routine anyhow, since the result would be lost when returning. Not sure, if you really want to do like this, because of losing the result.

      This would be a typical reason to use a pointer instead. You need to check the value of the pointer internally for zero value.

      void foo(QString cmd, QDataStream *s = 0);
      

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • FranckynosF Offline
        FranckynosF Offline
        Franckynos
        wrote on last edited by
        #3
        void foo(QString cmd, const QDataStream &s = QDataStream());
        

        This way doesn't work.

        i would like used reference and not pointer...
        but this works

        void foo(QString cmd, QDataStream *s = 0);
        

        it's not possible to use references ?

        K 1 Reply Last reply
        0
        • FranckynosF Franckynos
          void foo(QString cmd, const QDataStream &s = QDataStream());
          

          This way doesn't work.

          i would like used reference and not pointer...
          but this works

          void foo(QString cmd, QDataStream *s = 0);
          

          it's not possible to use references ?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Franckynos said in QDataStream default in function argument:

          void foo(QString cmd, const QDataStream &s = QDataStream());
          

          This way doesn't work.

          Why does it not work?
          Do you have an error message or you do not like it?

          You have to decide whether you like to use a reference or a pointer. With both you are able to retrieve the result generated in a routine. However, if you are setting a default you got a problem with a reference, because the result will not be passed back respectively you cannot access anyhow.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            You already used a reference: void foo(QString cmd, QDataStream &s); and as @koahnig wrote, it makes no sense to try to assign a default value to the stream parameter.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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