Results 1 to 3 of 3

Thread: Base class and Derived classes (inheritance question)

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Base class and Derived classes (inheritance question)

    Hi I would like to know if it is possible to execute a function from a derived class that inherits from a base class and trigger it on all the derived classes.

    For example I have a base class with a generic template widget (for user interface), it has several functionality that can be called by the derived classes.

    Then I have a class that inherits this user interface.

    If for example I call the protected function of the baseclass through the instance of the derived class, is there a way that I can trigger it for all derived classes that inherit the function of the base class?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Base class and Derived classes (inheritance question)

    Quote Originally Posted by ehnuh View Post
    Hi I would like to know if it is possible to execute a function from a derived class that inherits from a base class and trigger it on all the derived classes.
    With an exception of static methods, you execute functions on objects and not classes, therefore for this to work, each instance has to be registered somewhere. If you keep such a register in the base class then I can imagine this working (I have no idea why you'd want to do that though). Here is an example:

    Qt Code:
    1. class BaseClass {
    2. public:
    3. BaseClass() {
    4. m_instances << this;
    5. }
    6. ~BaseClass() {
    7. m_instances.removeOne(this);
    8. }
    9. virtual void someMethod() {
    10. // ...
    11. foreach(BaseClass *instance, m_instances) {
    12. if(instance != this) instance->someOtherMethod(); // or someMethod() but you have to make sure it is overridden correctly in each subclass or you'll get incorrect behaviour
    13. }
    14. // ...
    15. }
    16. virtual void someOtherMethod() {
    17.  
    18. }
    19. private:
    20. static QList<BaseClass*> m_instances; // register of instances
    21. };
    22.  
    23. class SubClass : public BaseClass {
    24. public:
    25. SubClass() : BaseClass() {}
    26. virtual void someOtherMethod() { ... }
    27. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Base class and Derived classes (inheritance question)

    I think in Qt there's a possibility of doing this using singal and slots. Emitting a signal when the base class function is called, where the signal -in all instances of the derived class- is connected to a slot which calls the base class function. The signal can carry a variable which informs the slot that it should not emit the signal again to prevent a loop.

Similar Threads

  1. Containers with derived classes? (QMap or QList)
    By CSmith in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2012, 17:42
  2. Replies: 3
    Last Post: 6th April 2012, 16:44
  3. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 12:32
  4. Replies: 3
    Last Post: 27th December 2008, 19:34
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.