Results 1 to 11 of 11

Thread: Inheritance of a static object factory

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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).

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.