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. Why is there no copying at QNetworCookieJar?
Forum Updated to NodeBB v4.3 + New Features

Why is there no copying at QNetworCookieJar?

Scheduled Pinned Locked Moved General and Desktop
26 Posts 4 Posters 12.2k 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #16

    Why do you need more than one?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      haithun
      wrote on last edited by
      #17

      I dont need more than one but I want to give my function "SetCookieJar" a QNetworkCookie. Thats data should be saved in my extended class. I am doing this because I want to avoid to have two member variables and the different treatment in my functions.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #18

        So as Andre wrote - subclass QNetworkCookieJar, make the two protected functions public and use them to transfer the cookie data. No need for a copy constructor as far as I see. Left alone that a copy constructor would do quite the same.

        @
        this->setAllCookies(other->allCookies());
        @

        Or implement a cloneFromOther Method to simulate an assignment operator/copy constructor:

        @
        void MyCookieJar::cloneFromOther(const QNetworkCookieJar *other)
        {
        if(other)
        setAllCookies(other->allCookies())
        }
        @

        As mentioned before, QNetworkCookieJar is a QObject and there simply IS NO copy constructor. It does not help to ask again and again - the trolls will not add it :-)

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • H Offline
          H Offline
          haithun
          wrote on last edited by
          #19

          I understood that there is no copy cunstructor and also the reason.
          Well your code is very similar to the one that I wrote cause subclassing QNetworkCookieJar was also my idea. But your version wont work, because you try to call the protected "allCookies()" function of the "other" object as if it's public. Would it be public my problem would not exist.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #20

            If you derive class MyCookieJar from QNetworkCookieJar you have access to the protected methods of the base class QNetworkCookieJar. And of course you have access to the protected methods of the other object pointer in that method.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • H Offline
              H Offline
              haithun
              wrote on last edited by
              #21

              Well I tried to do it in a constructor of my class but it wont work.

              Here the class with constructor:
              @
              class MyClass : public QNetworkCookieJar
              {
              Q_OBJECT
              public:

              CopyFromCookieJar(QNetworkCookieJar *Cookies, QObject *parent = 0) : NetworkCookieJar(parent)
              {
                  this->setAllCookies(Cookies->allCookies());
              }
              

              @

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #22

                The code fragment wouldn't even compile:

                Your class is named "MyClass", your Constructor is named "CopyFromCookieJar" - both names must be equal.

                Also the argument of the constructor should be
                @const QNetworkCookieJar *Cookies@

                Additionally, you should check for a null pointer before calling any methods on the pointer.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  haithun
                  wrote on last edited by
                  #23

                  Yeah sry I just changed the name of the function and forgot that it should be a constructor.

                  Why do you declare the pointer as const?

                  The reason of the compiler is "'QList<QNetworkCookie> QNetworkCookieJar::allCookies() const' is protected".

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #24

                    Sorry for the wrong guidance. You cannot access the protected method of the base class in a derived class using a base class pointer.

                    You must replace all appearences of QNetworkCookieJar with your derived class in your application and pass around only pointers/references of this derived class. Then you have access to the protected methods.

                    @
                    class MyCookieJar : public QNetworkCookieJar
                    {
                    Q_OBJECT
                    public:
                    explicit MyCookieJar(QObject *parent = 0)
                    : QNetworkCookieJar(parent)
                    {
                    // nothing
                    }

                    void cloneFromOther(const MyCookieJar *other) {
                        setAllCookies(other->allCookies());
                    }
                    

                    };
                    @

                    For the const pointer:
                    the other cookie jar will not be changed by the clone method (or the copy constructor), so it's actually a good idea to make the pointer const. There are good explanations on const correctnes on the "C++ FAQ":http://www.parashift.com/c++-faq-lite/const-correctness.html

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      haithun
                      wrote on last edited by
                      #25

                      Yeah but now we are right there where I started cause I did not want to pass my derived class. I want to pass a QNetworkCookieJar to the function and then I want to put the cookies into my derived class. The reason why I want to have just Qt classes is no real reason it's just that I hate it to include the header in every upcoming projects.

                      I guess my first version by using a reinterpretate cast is the only solution.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #26

                        Then there's no solution to your problem - you can't have your cake and eat it too :-)

                        I would go for the subclass approach. reinterpret_cast is ugly and should be avoided whenever theres a more elegant (and save!) solution - just my 0,02 € - :-)

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        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