Best way to initialize QList?
-
Hey (this is Xander84, my other account is corrupted or something I can't use it atm, that's why I wasn't answering sooner, sorry).
-
"I’m assuming ‘override’ tells the compiler to skip over this definition and use another one (like the one I created)?"
override tells the compiler that it has to keep the "contract" with the method of the base class. So I'would say it is exactly the opposite of what you said. :D
the override keyword is optional and does nothing really, all it is used for is helping the developer overriding the correct method and not (what you said) creating new ones by accident. If you have ever done some Java there is a @Override annotation with the same effect:
@
@Override void foo() { } // java
// similar to c++11 override keyword
void foo() override { } // c++11
@ -
the virtual keyword "just" indicates that a method can be overridden by child classes, without declaring a method "virtual" you cannot override it in c++ (unlike other programming langues, e.g. Java is everything virtual unless you define it "final")
-
about your QList question, in believe in Qt you cannot save references in any of the container classes. e.g.
@
QList<QString&> invalidList;
@
that is meant by list of references, since you can't do that I don't know how you would add a reference to its own member if you only use plain objects (copies) or pointers... -
on your GitHub page you are still missing the implementation of historyContains in your nbhistory.cpp file? I see the definition in the header only, that is a syntax error in c++. Maybe not that obvious to you why? your are defining a non-virtual abstract method, which is impossible to implement by any child classes, so it's a syntax error. The virtual keyword you used is only for the next method:
@
virtual
void addHistoryEntry(const QString & url) = 0;
bool historyContains(const QString &url) const = 0;
// format as
virtual void addHistoryEntry(const QString & url) = 0;
bool historyContains(const QString &url) const = 0;
@
virtual is not like the visibility modifier (public, private, etc), there is no following ":".
anyway you don't need a a virtual there, unless you want to override the method in a child class later (I guess you don't have any), so just remove it. You should know what virtual means by now since I briefly explained it above, if not just search for it on the net.
Also "= 0" after a method definition means the method is abstract (has no implementation body at all), so you should remove that too, you cannot have abstract methods if you want to create objects of that class.
That said, just do it like this:
@
void addHistoryEntry(const QString & url) override;
bool historyContains(const QString &url) const override;
@
and provide implementations for both methods in your cpp file. :)
if you don't have c++11 remove the override keyword there, tip to compile your project with c++11 support you onyl need to tell Qt to use it, most modern compilers should be able to support it. in your qmake project file add:
@
CONFIG += c++11
@
that only tells qmake to use the compiler flags (not necessary for all compilers), but with this config it works on all supported compiler by Qt.
(sorry for the long text again)
-
-
Ok so I have since converted NBhistoryManager into a subclass of QObject. This way I don't have to deal with overridden functions and all that jazz. I am still having trouble with the program crashing when it calls QList.prepend(). The implementation of the m_historyTitle has not been changed.