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 7 1400
"Dave Townsend" <da********@comcast.net> wrote in message
news:fq********************@comcast.com... 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.
I don't understand why this approach doesn't work. Would a Singleton with a
getDefaultQPixmax() fix the problem?
So right now I've gone back to using pointers instead of references, but
I'm unhappy about that. Any ideas ?
Have you considered avoiding operator[] (which is what I assume is causing a
problem) and instead using find/insert? If a default constructor doesn't
make sense for your Foo class, then operator[] may be the wrong member to
use for accessing and inserting elements in the std::map. IIRC, item 24 in
"Effective STL" by Meyers had example code that showed a simple way to avoid
operator[].
--
David Hilsee
"David Hilsee" <da*************@yahoo.com> wrote in message
news:1c********************@comcast.com... "Dave Townsend" <da********@comcast.net> wrote in message news:fq********************@comcast.com... 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.
I don't understand why this approach doesn't work. Would a Singleton with
a getDefaultQPixmax() fix the problem?
So right now I've gone back to using pointers instead of references, but I'm unhappy about that. Any ideas ?
Have you considered avoiding operator[] (which is what I assume is causing
a problem) and instead using find/insert? If a default constructor doesn't make sense for your Foo class, then operator[] may be the wrong member to use for accessing and inserting elements in the std::map. IIRC, item 24
in "Effective STL" by Meyers had example code that showed a simple way to
avoid operator[].
-- David Hilsee
Ok. I'd thought about the Singleton, but I'd always thought of singletons
returning
pointers to object, but I now realize you can return a reference too.
I think the suggestion about avoiding the operator[] is probably best - I
only created the
default constructor to please the MSVC compiler, if there's a way of
avoiding operator[]
that would be better since copying these objects doesn't make a lot of sense
anyway.
thanks,
dave.
"Dave Townsend" <da********@comcast.net> wrote in message
news:zN********************@comcast.com...
<snip> I think the suggestion about avoiding the operator[] is probably best - I only created the default constructor to please the MSVC compiler, if there's a way of avoiding operator[] that would be better since copying these objects doesn't make a lot of
sense anyway.
Copying, or contructing via a default constructor? I was commenting on the
default constructor, not on the copy constructor or the assignment operator.
--
David Hilsee
"Dave Townsend" <da********@comcast.net> wrote in message
news:zN********************@comcast.com... "David Hilsee" <da*************@yahoo.com> wrote in message news:1c********************@comcast.com... "Dave Townsend" <da********@comcast.net> wrote in message news:fq********************@comcast.com... > 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. >
Since the pixmap pointer/reference is private why do you care if it is
implemented as a pointer or reference? Your users certainly won't care. Just
do whatever comes easiest. Why not use references externally but pointers
internally, like this
class Foo
{
public:
Foo() : _pix(0) {}
Foo( ..... QPixmap& pix .....) : _pix(&pix) {}
private:
QPixmap* _pix;
};
Having a default constructor can be useful for reasons other than using
map::operator[].
john
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2r*************@uni-berlin.de... "Dave Townsend" <da********@comcast.net> wrote in message news:zN********************@comcast.com... "David Hilsee" <da*************@yahoo.com> wrote in message news:1c********************@comcast.com... "Dave Townsend" <da********@comcast.net> wrote in message news:fq********************@comcast.com... > 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. > Since the pixmap pointer/reference is private why do you care if it is implemented as a pointer or reference? Your users certainly won't care.
Just do whatever comes easiest. Why not use references externally but pointers internally, like this
class Foo { public: Foo() : _pix(0) {} Foo( ..... QPixmap& pix .....) : _pix(&pix) {} private: QPixmap* _pix; };
Having a default constructor can be useful for reasons other than using map::operator[].
john
Yes, I considered that situation, but I thought it was a blemish and ugly.
I've found
using references is a more robust way of doing things, so I tend to opt for
that approach.
A default constructor in the particullar situation is probably not a good
idea, these objects don't
really have a default state, so that was a mistake in my modelling of them.
Its probably
a good idea to forbid the assignment of these objects ( they are similar to
widgets or Windows
objects, which are not copyable (sp ?) , so users of the class don't tread
on that booby trap.
dave
"Dave Townsend" <da********@comcast.net> wrote in message
news:mu********************@comcast.com... "John Harrison" <jo*************@hotmail.com> wrote in message news:2r*************@uni-berlin.de... "Dave Townsend" <da********@comcast.net> wrote in message news:zN********************@comcast.com... "David Hilsee" <da*************@yahoo.com> wrote in message news:1c********************@comcast.com... > "Dave Townsend" <da********@comcast.net> wrote in message > news:fq********************@comcast.com... > > 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. > > Since the pixmap pointer/reference is private why do you care if it is implemented as a pointer or reference? Your users certainly won't care.
Just do whatever comes easiest. Why not use references externally but
pointers internally, like this
class Foo { public: Foo() : _pix(0) {} Foo( ..... QPixmap& pix .....) : _pix(&pix) {} private: QPixmap* _pix; };
Having a default constructor can be useful for reasons other than using map::operator[].
john
Yes, I considered that situation, but I thought it was a blemish and ugly. I've found using references is a more robust way of doing things, so I tend to opt
for that approach.
A default constructor in the particullar situation is probably not a good idea, these objects don't really have a default state, so that was a mistake in my modelling of
them. Its probably a good idea to forbid the assignment of these objects ( they are similar
to widgets or Windows objects, which are not copyable (sp ?) , so users of the class don't tread on that booby trap.
OK but one of your requirements was to use the object in a std::map. IIRC
it's a requirement that classes in any STL container must implement the
assignment operator.
Also using references makes it impossible to implement a swap member
function; which is useful even for objects which are not copyable.
john
"Dave Townsend" <da********@comcast.net> wrote in message news:<fq********************@comcast.com>... 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;
};
Where, and in what type of container if any, are the shared objects stored?
This is an important part of your problem IMO. 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.
Can't you have a default pixmap object adjacent to the other pixmap objects? So right now I've gone back to using pointers instead of references, but I'm unhappy about that. Any ideas ?
When using pointers where are these shared pixmap objects stored? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Smith |
last post by:
Can someone point me to an example of how to implement and access the kind
of object shown below?
Most of the examples if found are an object that contains one other object.
I need to create an...
|
by: Aguilar, James |
last post by:
Hey guys. A new question:
I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. ...
|
by: Kenneth Baltrinic |
last post by:
I am trying to compare values coming out of a database record with known
default values. The defaults are in an array of type object (because they
can be of any basic data type, I am not working...
|
by: Andrew Quine |
last post by:
Hi
Just read this article http://www.artima.com/intv/choices.html.
Towards the end of the dicussions, when asked "Did you consider
including support for the concept of immutable directly in C#...
|
by: CQ |
last post by:
Hi there,
I am having the following problem: I have the following class:
class reachGraphState {
protected:
/* ... some stuff .... */
public:
|
by: rpsetzer |
last post by:
I have to create a big web application and I was thinking of using a
data layer. For each entity in the database, I'll define a class that
maps the table structure, having sub-objects for each...
|
by: sachingoel82 |
last post by:
Hi All,
I find following C++ behaviour regarding temporary objects quite
contradictory. Consider this:
class A
{
...
public:
|
by: Subodh |
last post by:
Hi All,
In C++ we could pass a constant reference to a function so that the
target function could not modify the objects passed
eg. const classA & dummmyfunction(const classB)
similar thing...
|
by: Juha Nieminen |
last post by:
I'm sure this is not a new idea, but I have never heard about it
before. I'm wondering if this could work:
Assume that you have a common base class and a bunch of classes
derived from it, and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |