Why is there no copying at QNetworCookieJar?
-
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()); }
@
-
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.
-
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".
-
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 -
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.
-
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 € - :-)