PDA

View Full Version : Data structures with qt4



NewLegend
25th July 2010, 15:05
Hello
This is an example of data structures with qt4

You enter a set of ID
And then print all the IDs

squidge
25th July 2010, 18:24
What? Data structures in Qt is exactly the same as data structures in C++, unless you use a Qt specific object to hold the data, which you dont.

Secondly, what you are doing is just reinventing the wheel. There are much better (less code, more readable) ways to do what you have done.

NewLegend
25th July 2010, 19:14
What? Data structures in Qt is exactly the same as data structures in C++, unless you use a Qt specific object to hold the data, which you dont.

Secondly, what you are doing is just reinventing the wheel. There are much better (less code, more readable) ways to do what you have done.

Where is this code ?
The least number of lines and more accurate.

There is difficulty in building data structures with qt4. If you use inheritance in data structures.

squidge
25th July 2010, 20:30
Well, for example:

"QList<T> is one of Qt's generic container classes. It stores a list of values and provides fast index-based access as well as fast insertions and removals."

Is far easier to use and more manageable than your example data structure.

SixDegrees
25th July 2010, 21:55
You can also simply use the STL std::list<T> class, which also has the advantage of being part of the STL's container system. This allows users to swap one container for another without changes to their code, as the API is the same across all classes for the most part.

wysota
25th July 2010, 21:57
The least number of lines and more accurate.
The number of lines of code is not that relevant, consider the two examples:


struct one {
QString a;
QString b;
};


struct two {
two(const QString &_a, const QString &_b) : m_a(_a), m_b(_b){}
void setA(const QString &a) { m_a = a; }
void setB(const QString &b) { m_b = b; }
const QString &a() const { return m_a; }
const QString &b() const { return m_b; }
private:
QString m_a;
QString m_b;
};

Which is better? From what point of view? And how about this one?


struct three_private : public QSharedData {
QString a;
QString b;
three_private() : QSharedData(){}
three_private(const three_private &other) : QSharedData(other), a(other.a), b(other.b){}
};

class three {
public:
three() : d(new three_private){}
three(const three &other) : d(other.d){}
void setA(const QString &a) { d->a = a; }
void setB(const QString &b) { d->b = b; }
const QString &a() const { return d->a; }
const QString &b() const { return d->b; }
private:
QSharedDataPointer<three_private> d;
};

Which is fastest? Which is least error prone? Which is easiest to extend, i.e. by forcing "b" to be lowercase alphanumeric characters only?


There is difficulty in building data structures with qt4. If you use inheritance in data structures.
You can use C structures in Qt-based code as well, if you want and you like to inflict pain on yourself while programming :)

Edit: Oh, for completeness regarding your code, there is QLinkedList.