D
Dave Townsend
Hi,
I came across a little problem the other day, I wonder
if anyone has a more elegant solution:
I have a class which has some "pixmap" members, this is
a QT-data type which is similar to a bitmap on Windows.
A number of the objects are created and share the same set of
pixmaps, so a reference I thought would be a good thing here.
I thought it would be a good thing to
class Foo
{
public:
Foo( ..... QPixmap& pix .....)
_pix(pix)
{}
private:
QPixmap& _pix;
};
Ok, this worked fine. Then I wanted to use an STL Map , this
is where the trouble began since I needed to have a default constructor to
satisfy
the map template, but how do I handle that? I tried some thing like this:
Foo()
:_pixmap( QPixmap() )
{}
but this is bogus since the temporary will be destroyed after the
constructor finishes.
I thought I could create a static Qpixmap which is just used to satisfy the
constructor:
static QPixmap _defaultPix;
Foo()
:_pixmap( _defaultPix)
{}
However, this will not work - the Pixmap cannot be constructed correctly
until other
parts of the application framework have been initialized , instead the
constructor throws and kills the application.
So right now I've gone back to using pointers instead of references, but I'm
unhappy
about that. Any ideas ?
dave
I came across a little problem the other day, I wonder
if anyone has a more elegant solution:
I have a class which has some "pixmap" members, this is
a QT-data type which is similar to a bitmap on Windows.
A number of the objects are created and share the same set of
pixmaps, so a reference I thought would be a good thing here.
I thought it would be a good thing to
class Foo
{
public:
Foo( ..... QPixmap& pix .....)
_pix(pix)
{}
private:
QPixmap& _pix;
};
Ok, this worked fine. Then I wanted to use an STL Map , this
is where the trouble began since I needed to have a default constructor to
satisfy
the map template, but how do I handle that? I tried some thing like this:
Foo()
:_pixmap( QPixmap() )
{}
but this is bogus since the temporary will be destroyed after the
constructor finishes.
I thought I could create a static Qpixmap which is just used to satisfy the
constructor:
static QPixmap _defaultPix;
Foo()
:_pixmap( _defaultPix)
{}
However, this will not work - the Pixmap cannot be constructed correctly
until other
parts of the application framework have been initialized , instead the
constructor throws and kills the application.
So right now I've gone back to using pointers instead of references, but I'm
unhappy
about that. Any ideas ?
dave