473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

objects with member references..

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



Jul 22 '05 #1
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
Jul 22 '05 #2

"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.

Jul 22 '05 #3
"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
Jul 22 '05 #4

"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
Jul 22 '05 #5

"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

Jul 22 '05 #6

"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
Jul 22 '05 #7
"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?
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
9
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. ...
8
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...
48
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#...
12
by: CQ | last post by:
Hi there, I am having the following problem: I have the following class: class reachGraphState { protected: /* ... some stuff .... */ public:
8
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...
1
by: sachingoel82 | last post by:
Hi All, I find following C++ behaviour regarding temporary objects quite contradictory. Consider this: class A { ... public:
3
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...
15
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.