Well, I would like it to be QList<Object>, because I want to add stuff in Object relative to LibObject..
Well, I would like it to be QList<Object>, because I want to add stuff in Object relative to LibObject..
But LibObject::createObject() returns a LibObject, not an Object. You can make it return an Object if you want it to.
Please explain. Do I have to re-declare the createObject() method in the Object class definition?
I don't think so.
A base class factory method can (and often does) return an instance of a derived class. You would just change LibObject::createObject to:
Qt Code:
Object LibObject::createObject() { Object o; return o; }To copy to clipboard, switch view to plain text mode
There's an excellent example of something similar here (in the "after" section): http://sourcemaking.com/design_patte...y_method/cpp/1
No way! But then, shouldn't LibObject (the base class) be aware of its subclass (Object)? I think I don't exactly understand it yet, but I'll read your reference. Thanks!
It blew my mind too.
It should be aware of the subclass, in the sense that it needs to #include the subclass's header file (or they could be in the same header file).
Bookmarks