PDA

View Full Version : Strange behaviour of QStringList inheritance



CuteBeginner
11th February 2013, 19:44
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:



// class header declaration
class ArgumentList : public QStringList {
public:
ArgumentList();

// other function definitions etc
};

// constructor definition

ArgumentList :: ArgumentList(){
if (qApp != NULL)
// this is the statement I'm questioning
*this = qApp->arguments();
}


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.

Santosh Reddy
11th February 2013, 20:09
Your question is not clear to me, but if your question is about the assignment operator, it will invoke base class assignment operator.

CuteBeginner
11th February 2013, 21:17
Thanks Santosh,

What I mean is as in following use of the ArgumentList class:


#include <QCoreApplication>
#include <QDebug>
#include "argumentlist.h"

int main (int argc, char* argv[]) {

ArgumentList* list = new ArgumentList;
qDebug() << "list->getSwitchArg();
// other statements etc
....
return 0;
}



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();

ChrisW67
11th February 2013, 21:53
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.