Results 1 to 4 of 4

Thread: Strange behaviour of QStringList inheritance

  1. #1
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Strange behaviour of QStringList inheritance

    Well firstly it could just be me missing a point about c++ inheritance. I am reading this code from a textbook and I am surprised why it works when I'm thinking it shouldn't.
    Here goes the code snippet:

    Qt Code:
    1. // class header declaration
    2. class ArgumentList : public QStringList {
    3. public:
    4. ArgumentList();
    5.  
    6. // other function definitions etc
    7. };
    8.  
    9. // constructor definition
    10.  
    11. ArgumentList :: ArgumentList(){
    12. if (qApp != NULL)
    13. // this is the statement I'm questioning
    14. *this = qApp->arguments();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Now I know qApp->arguments() returns a QStringList, but why is it still possible to call other ArgumentList functions later on when we have now replaced it with a QStringList with the assignment statement.

    I have tested my thinking in a similar class and and after assigning a QStringList, I cant access functions defined in my QStringList inherited class. Please help explain this to me as I don't seem to get it.

    Many thanks for your help.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Strange behaviour of QStringList inheritance

    Your question is not clear to me, but if your question is about the assignment operator, it will invoke base class assignment operator.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Aug 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange behaviour of QStringList inheritance

    Thanks Santosh,

    What I mean is as in following use of the ArgumentList class:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include "argumentlist.h"
    4.  
    5. int main (int argc, char* argv[]) {
    6.  
    7. ArgumentList* list = new ArgumentList;
    8. qDebug() << "list->getSwitchArg();
    9. // other statements etc
    10. ....
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 


    Why I am I able to access the "QString getSwitchArg() " method which is defined in ArgumentList class when its constructor assigned it to a QStringList with this statement:
    if (qApp != NULL)
    *this = qApp->arguments();

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

    Default Re: Strange behaviour of QStringList inheritance

    You can call ArgumentList functions on an instance of ArgumentList; I don't know why you find that is surprising in any way. Your constructor has copied the application argument list into the QStringList underlying the instance of ArgumentList. It has not suddenly turned the instance of ArgumentList into a bare instance of QStringList.

Similar Threads

  1. strange behaviour with Qt key_space
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 10th April 2010, 00:21
  2. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 05:03
  3. qinputdialog strange behaviour
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2008, 20:29
  4. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 18:38
  5. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 07:17

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.