Ok, it seems like it would be worth it, if they can help the model divine the information they want. And, if they go away, they're not using up any resources outside of the Qt-required context.The point is it's not worth it to have shortlived objects owned by indexes. There are really no benefits of that.
Again, I don't think it's bad. The MIs are short lived, there's no reason a tag-along piece of information can't be just as short-lived. The best analogy I could come up with was the 'big pointer' analogy - they're not model data, per se. And, if the memory manager is just going to dole out the same pointers from a pool, so much the better.That's bad then. The data remains valid as long as the model doesn't change, thus there is no point in deleting it just to create it again in a moment. If you want such volatile cache - implement it in the model, it knows better when data can or has to be destroyed.
(However: your index.parent().parent()... example in another thread is something to think about. Nothing better than going back to the original data - can't argue that.)
Stack Allocator
So, I walked through 'createIndex()' to see what it does. Nothing particularly magical. As you say, it creates the struct on the stack. The trick is that it relies on the copy constructor to fill each instance up the stack. Makes sense in a 'slap-your-forehead', 'of-course!' sort of way, when you see it.
I created an example that uses the same construct to play with - you can watch the values being copied up the stack (in the debugger) as each frame backs out.
A curiosity, though - I'm watching the destructor. I would expect the destructor to get called as it backs out of each frame (the frame's instance gets destroyed.) Instead, it only gets destroyed when the calling instance backs out. That's odd. It's almost as if there's a ref count on it.
Tried both ms and gnu compilers - same behavior. Here's the code, if you're interested:
Qt Code:
#include <iostream> using namespace std; class TT { friend class A; public: ~TT() { cout << "Destroyed!!" << endl; } int A() { return a; } int B() { return b; } int C() { return c; } int D() { return d; } private: int a, b, c, d; TT(int _a, int _b, int _c, int _d) { a = _a, b = _b, c = _c, d = _d; } }; class A { public: TT createTT(int a, int b, int c, int d) { return TT(a,b,c,d); } void init_A() { TT item = getItemFromB(); cout << "TT item: " << item.A() << ", " << item.B() << ", " << item.C() << ", " << item.D() << endl; } virtual TT getItemFromB() = 0; }; class B : public A { public: virtual TT getItemFromB() { return createTT(1, 2, 3, 4); } }; int main (int argc, char **argv) { B b; b.init_A(); return 0; }To copy to clipboard, switch view to plain text mode
Anyway, I need to get on to my little demo and try to get it coded up before the universe collapses.
Thanks for the discussion. As always, C++ (and Qt) gives me something new to chew on when I dig under the covers.
Cheers,
rickb
Bookmarks