Design a new class or inheritance or else?

J

Jack

Dear all,

I'm using a commercial library that I cannot modify. There's this one
class in the library which is almost 90% usable. But unfortunately, I
have to reimplement 3 member functions. The problem is the original
class is not designed very well, because it is assumed to be the final
class. Because I have to modify some private members in the
reimplemented functions. What should I do? Design a new class or doing
something else?

Here is the detailed information:
I have to design a Label class which can show both bitmap and text.
I'm using QT as my base library. QT has a class named QLabel, but it
only supports either bitmap or text. QLabel has two virtual functions,
one is setText, and another is setPixmap, but they eliminate the other
one when being called. So I have to reimplement the two functions. But
the problem is the text and bitmap members are private in QT, and
there's no other functions to set the values other than setText and
setPixmap. So what should I do? Simply copy the code and design a new
class according to QLabel or else?

Thanks in advance!
Jack
 
I

Ivan Vecerina

....
I have to design a Label class which can show both bitmap and text.
I'm using QT as my base library. QT has a class named QLabel, but it
only supports either bitmap or text. QLabel has two virtual functions,
one is setText, and another is setPixmap, but they eliminate the other
one when being called. So I have to reimplement the two functions. But
the problem is the text and bitmap members are private in QT, and
there's no other functions to set the values other than setText and
setPixmap. So what should I do? Simply copy the code and design a new
class according to QLabel or else?

I would recommend replicating the code and making your own parallel
implementation.
This is probably the least likely to 'break' with later versions of your
library, and will give you more freedom if you later need to make additional
changes (e.g. controlling the relative placement of the text and image).

hth -Ivan
 
D

Daniel T.

Dear all,

I'm using a commercial library that I cannot modify. There's this one
class in the library which is almost 90% usable. But unfortunately, I
have to reimplement 3 member functions. The problem is the original
class is not designed very well, because it is assumed to be the final
class. Because I have to modify some private members in the
reimplemented functions. What should I do? Design a new class or doing
something else?

Here is the detailed information:
I have to design a Label class which can show both bitmap and text.
I'm using QT as my base library. QT has a class named QLabel, but it
only supports either bitmap or text. QLabel has two virtual functions,
one is setText, and another is setPixmap, but they eliminate the other
one when being called. So I have to reimplement the two functions. But
the problem is the text and bitmap members are private in QT, and
there's no other functions to set the values other than setText and
setPixmap. So what should I do? Simply copy the code and design a new
class according to QLabel or else?

I would use two QLabel objects. Put them both in a single class that is
in charge of making sure they are properly positioned relitive to each
other.
 
S

Stephan Br?nnimann

Dear all,

I'm using a commercial library that I cannot modify. There's this one
class in the library which is almost 90% usable. But unfortunately, I
have to reimplement 3 member functions. The problem is the original
class is not designed very well, because it is assumed to be the final
class. Because I have to modify some private members in the
reimplemented functions. What should I do? Design a new class or doing
something else?

Here is the detailed information:
I have to design a Label class which can show both bitmap and text.
I'm using QT as my base library. QT has a class named QLabel, but it
only supports either bitmap or text. QLabel has two virtual functions,
one is setText, and another is setPixmap, but they eliminate the other
one when being called. So I have to reimplement the two functions. But
the problem is the text and bitmap members are private in QT, and
there's no other functions to set the values other than setText and
setPixmap. So what should I do? Simply copy the code and design a new
class according to QLabel or else?

Thanks in advance!
Jack

Though this is a bit off-topic:

Create your own widget (derived from QWidget or QFrame (*)) and give it
2 members QLabel: one carrying the bitmap and one carrying the text.
Create a nice, small custom widget and register is designer.

(*) depends if you need signals or not.

Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top