-2

Here is the code (from An introduction to design pattern in c++ with qt4), on line 9, i guess that QString(other) gives an QString object, but what is initialized here?

#include <QList> #include <QtAlgorithms> // for qSort() #include <QStringList> #include <QDebug> class CaseIgnoreString : public QString { public: CaseIgnoreString(const QString& other = QString()) : QString(other) {} // bool operator<(const QString & other) const { return toLower() < other.toLower(); } bool operator==(const QString& other) const { return toLower() == other.toLower(); } }; int main() { CaseIgnoreString s1("Apple"), s2("bear"), s3 ("CaT"), s4("dog"), s5 ("Dog"); Q_ASSERT(s4 == s5); Q_ASSERT(s2 < s3); Q_ASSERT(s3 < s4); QList<CaseIgnoreString> namelist; namelist << s5 << s1 << s3 << s4 << s2; /* Insert all items in an order that is definitely not sorted. */ qSort(namelist.begin(), namelist.end()); int i=0; foreach (const QString &stritr, namelist) { qDebug() << QString("namelist[%1] = %2") .arg(i++).arg(stritr) ; } QStringList strlist; strlist << s5 << s1 << s3 << s4 << s2; /* The value collection holds QString but, if you add CaseIgnoreString, a conversion is required. */ qSort(strlist.begin(), strlist.end()); qDebug() << "StringList sorted: " + strlist.join(", "); return 0; } 
1
  • 6
    It is initializing the base class to be a copy of the constructor argument. Commented Feb 3, 2014 at 14:55

2 Answers 2

1

It's not an initialization of a member but an explicit call to the initialization of the base class. See Why explicitly call a constructor in C++ for more explanation.

Sign up to request clarification or add additional context in comments.

Comments

0

In this constructor

CaseIgnoreString(const QString& other = QString()) : QString(other) {} // 

there is a default argument for parameter other. If the user will not specify an argument then the compiler will initialize the parameter with a temporary object created by calling constructor QString(). And this parameter is used to initialize the base class of CaseIgnoreString by using the ctor initializer list

: QString(other) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.