Results 1 to 11 of 11

Thread: Inheritance of a static object factory

  1. #1
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Inheritance of a static object factory

    Dear all,

    Could someone please explain what I am doing wrong below, and why this is not working?

    Thanks!

    In a dynamic library i have a class LibObject that has a static function createObject() which returns an instance of itself:
    Qt Code:
    1. class LibObject
    2. {
    3. private:
    4. int a;
    5.  
    6. public:
    7. LibObject();
    8. static LibObject createObject(); // the LibObject factory
    9. int getA();
    10. void setA(int i);
    11. }
    12.  
    13. LibOjcect::LibObject() {}
    14.  
    15. LibObject LibObject::createObject() { // the LibObject factory
    16. LibObject o;
    17. return o;
    18. }
    19.  
    20. int LibObject::getA() { return a }
    21.  
    22. void LibObject::setA(int i) { a = i }
    To copy to clipboard, switch view to plain text mode 
    Now in another class Object I am extending the LibObjectclass:
    Qt Code:
    1. class Object : public LibObject
    2. {
    3. public:
    4. Object();
    5. }
    6.  
    7. Object::Object() {}
    To copy to clipboard, switch view to plain text mode 
    Ok. So, now a third class would like to add Object objects to a QList:
    Qt Code:
    1. class Test
    2. {
    3. private:
    4. QList<Object> objects;
    5.  
    6. public:
    7. Test();
    8. }
    9.  
    10. Test::Test()
    11. {
    12. objects.append(Object::createObject());
    13. }
    To copy to clipboard, switch view to plain text mode 
    At compile time I get this error:
    error: no matching function for call to 'QList<Object>::append(LibObject)'

    So, is the static function createObject() not copied to the Object declaration?

  2. #2
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Inheritance of a static object factory

    Shouldn't that be "QList<LibObject>" in class Test?

  3. #3
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Inheritance of a static object factory

    Well, I would like it to be QList<Object>, because I want to add stuff in Object relative to LibObject..

  4. #4
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Inheritance of a static object factory

    But LibObject::createObject() returns a LibObject, not an Object. You can make it return an Object if you want it to.

  5. #5
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Inheritance of a static object factory

    Please explain. Do I have to re-declare the createObject() method in the Object class definition?

  6. #6
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Inheritance of a static object factory

    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:
    1. Object LibObject::createObject() {
    2. Object o;
    3. return o;
    4. }
    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

  7. #7
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Inheritance of a static object factory

    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!

  8. #8
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Inheritance of a static object factory

    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).

  9. #9
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Inheritance of a static object factory

    Cool! I'l try


    Added after 14 minutes:


    Hmm, the LibObstacle class cannot access class Object, because LibObject is a class in a shared library. And class Object resides in another application. What now??


    Added after 27 minutes:


    Maybe I should forget about using the static factory function? I could add a LibObject::setAllAttributes() function and in Test use something like
    Qt Code:
    1. Test::Test()
    2. {
    3. Object o;
    4. o.setAllAttributes();
    5. objects.append(o);
    6. }
    To copy to clipboard, switch view to plain text mode 


    Added after 9 minutes:


    Ok, that works. But it is not very elegant. What could be a better solution, still using the factory-pattern?
    Last edited by nomiz; 8th March 2012 at 21:30.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Inheritance of a static object factory

    You are storing actual instances in the list: LibObject and Object are not interchangable. Pointers to LibObjects and Objects could both be stored in a QList<LibObject*> because a base class pointer can point to an instance of a derived class:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class LibObject {
    5. public:
    6. virtual void saySomething() { qDebug() << "LibObject"; }
    7. };
    8.  
    9. class ObjectA: public LibObject {
    10. public:
    11. virtual void saySomething() { qDebug() << "ObjectA"; }
    12. };
    13.  
    14. class ObjectB: public LibObject {
    15. public:
    16. virtual void saySomething() { qDebug() << "ObjectB"; }
    17. };
    18.  
    19. class ObjectC: public LibObject {
    20. public:
    21. virtual void saySomething() { qDebug() << "ObjectC"; }
    22. };
    23.  
    24. // The factory method needs to know about all the classes but returns a pointer to the base class
    25. LibObject *createObject(int type)
    26. {
    27. switch(type) {
    28. case 0: return new ObjectA;
    29. case 1: return new ObjectB;
    30. case 2: return new ObjectC;
    31. }
    32. return 0;
    33. }
    34.  
    35. int main(int argc, char **argv)
    36. {
    37. QCoreApplication app(argc, argv);
    38.  
    39. // only need to know that a LibObject class exists to do this
    40. QList<LibObject *> list;
    41. list << createObject(0) << createObject(1) << createObject(2);
    42. foreach(LibObject *o, list)
    43. o->saySomething();
    44. qDeleteAll(list);
    45.  
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    May 2011
    Posts
    22
    Qt products
    Qt4

    Default Re: Inheritance of a static object factory

    Alright. Thanks for the explanation Chris!

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 15:29
  2. Problem with inheritance and object share use?
    By tonnot in forum General Programming
    Replies: 2
    Last Post: 1st August 2011, 18:08
  3. problem with constructor and inheritance QFile object
    By naturalpsychic in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 10:15
  4. static Object Vs Pointer
    By rajeshs in forum General Programming
    Replies: 4
    Last Post: 11th June 2008, 07:41
  5. Simple yet powerful object factory
    By mcostalba in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2007, 16:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.