PDA

View Full Version : using Qlist with a class



Havard
17th February 2007, 12:52
Hi.

I have a class without any constructors that I want to use Qlist with.
Since .append() does not take zero arguments, how do I add one more class to the Qlist?

Is Qlist the best container for me? The class contains all the info about the current Tab the user have selected using QTab, so I can use a MyClass[MyTab->currentIndex()] to access the current info. When the user adds another tab, I will need another instance of MyClass, and thought that Qlist and .append() would be the way to go, but I am having troubles with this since my class does not contain any constructors.

Any advice?

thanks!

Havard

jpn
17th February 2007, 13:03
Hi

The values stored in the various Qt containers can be of any assignable data type (http://doc.trolltech.com/4.2/containers.html#assignable-data-types).


To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator.

Havard
17th February 2007, 13:32
thanks, does this mean I have to equip my class with a default constructor, a copy constructor, and an assignment operator, even though it does not have (or need) any of this at the present?

Reading about "Generic Containers" in the QtAssistant, it says: "If we don't provide a copy constructor or an assignment operator, C++ provides a default implementation that performs a member-by-member copy. "

How come?

Havard

jacek
17th February 2007, 13:38
does this mean I have to equip my class with a default constructor, a copy constructor, and an assignment operator, even though it does not have (or need) any of this at the present?
You can avoid this by creating a list of pointers.


it says: "If we don't provide a copy constructor or an assignment operator, C++ provides a default implementation that performs a member-by-member copy. "

How come?
That's how C++ works. You can disable this by delaring those methods as private.

Havard
17th February 2007, 13:44
Originally Posted by Havard :
does this mean I have to equip my class with a default constructor, a copy constructor, and an assignment operator, even though it does not have (or need) any of this at the present?

You can avoid this by creating a list of pointers.


Ok, thanks! :) How do I then use the .append() since it needs an argument, and my class does not take arguments?

Havard

jacek
17th February 2007, 13:50
How do I then use the .append() since it needs an argument, and my class does not take arguments?

QList< Something * > list;

Something * something = new Something();
list.append( something );
// or simply:
list.append( new Something() );
Don't forget to delete the items when you remove them from the list (qDeleteAll( list ) might be helpful).

Havard
17th February 2007, 16:30
Thank you so much for your reply.

Unfortunately there is a problem with using pointers.
This little program illustrates this:

#include <QtCore/QCoreApplication>
#include <emmintrin.h>

class myClass {

public:
myClass();
~myClass();
__m128i O;

};

myClass::myClass()
{
//sets the O to 0 bits.
O = _mm_set_epi32 (0x00000000,0x00000000,0x00000000,0x00000000);
}

myClass::~myClass()
{
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<myClass *> list;
list.append(new myClass());

return a.exec();
}


Because I am using SSE2 instructions, the program crashes... However, if instead use: list[20]; there is no problem. But using *list[20]; the program crashes in the same manner.
If I replace: list.append(new myClass()); with list.append(&myClass()); it does not crash, but I don't think this is any good?

My question? How would I rewrite the program to allow for QList<myClass> list; instead of QList<myClass *> list;

Thank you!

Havard

jacek
17th February 2007, 16:56
Because I am using SSE2 instructions, the program crashes...
It doesn't crash on my system. Are there any error messages? What CPU do you have?


My question? How would I rewrite the program to allow for QList<myClass> list; instead of QList<myClass *> list;
Just change:
QList<myClass *> list;
list.append(new myClass());
to
QList<myClass> list;
list.append( myClass() );
The default implementations of operator= and copy constructor will be enough for myClass.

Havard
24th February 2007, 00:28
I just wanted to say that the problem was finally solved, but turned out to be a lot more complex than I had expected.

Without going into too much detail, it turns out that there is a problem with aligning the 16 byte sse2 variables inside a c++ class with a microsoft compiler.

So jacek, I am guessing that you are not using a microsoft-compiler since you did not get any errors with the little test-program I posted. I am even guessing you are not using anything microsoft (like Visual Studio) at all...? :)

The solution was to overload the "new" operator to make it align them properly.

Havard
24th February 2007, 10:32
Here is a link with the fix, if anyone would be interested in using sse2 with classes: http://softwarecommunity.intel.com/isn/Community/en-US/forums/post/30231301.aspx

jacek
24th February 2007, 19:38
So jacek, I am guessing that you are not using a microsoft-compiler since you did not get any errors with the little test-program I posted. I am even guessing you are not using anything microsoft (like Visual Studio) at all...? :)
Yes, you are absolutely right. :)