Calling member functions of a derived class - best approach?

G

gw7rib

I'm writing a program which has "notes" - these can appear on the
screen as windows with text in. It is possible to create an "index
note" - at present, this will contain a list of the titles (or other
data, you can choose) of some or all of the notes - you can choose the
selection criteria. Thus you can create notes to store any text you
want to, in a relatively free manner, but you can easily fish out all
the notes relating to some particular topic.

When the index note is present, it has an arrow pointing at one line.
If the index note has the focus, then pressing up will move the arrow
up a line, pressing down will move it down a line, and pressing control
right will move to the note corresponding to that line. The interface
is like that at present because I've converted the program from a DOS
one; the hope is to write a more Windowsy interface later.

At present all the index note stuff is done using globals. I would like
to try to move some of the code and data into the index note itself.
One way to do this would be to derive a class Indexnote from my
existing class Note, and put the extra code there. However, I am having
a moment of doubt as to the best way to call the additional code.

One way would be to have, say, functions goup, godown and gotonote as
part of Indexnote, and call them as required using a cast. For
instance,

case WM_KEYDOWN:
if (wParam == VK_UP && currentnote -> isindexnote()) (Indexnote *)
currentnote -> goup();

but I am not particularly happy using the cast. Presumably it could
also cause problems if I had further classes derived from Indexnote so
currentnote wasn't in fact merely an Indexnote.

The other approach would be to have the functions goup, godown and
gotonote as part of Note, but not doing anything in the base class.
Then I could do:

case WM_KEYDOWN:
if (wParam == VK_UP && currentnote -> isindexnote()) currentnote ->
goup();

but it seems a bit odd creating functions that have no real meaning.
The first approach is closer to what I have in my head.

So, can anyone advise as to the best technique, or suggest some other
way of doing it? Any other comments would be welcome as well.

TIA.
Paul.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I'm writing a program which has "notes" - these can appear on the
screen as windows with text in. It is possible to create an "index
note" - at present, this will contain a list of the titles (or other
data, you can choose) of some or all of the notes - you can choose the
selection criteria. Thus you can create notes to store any text you
want to, in a relatively free manner, but you can easily fish out all
the notes relating to some particular topic.

When the index note is present, it has an arrow pointing at one line.
If the index note has the focus, then pressing up will move the arrow
up a line, pressing down will move it down a line, and pressing control
right will move to the note corresponding to that line. The interface
is like that at present because I've converted the program from a DOS
one; the hope is to write a more Windowsy interface later.

At present all the index note stuff is done using globals. I would like
to try to move some of the code and data into the index note itself.
One way to do this would be to derive a class Indexnote from my
existing class Note, and put the extra code there. However, I am having
a moment of doubt as to the best way to call the additional code.

One way would be to have, say, functions goup, godown and gotonote as
part of Indexnote, and call them as required using a cast. For
instance,

case WM_KEYDOWN:
if (wParam == VK_UP && currentnote -> isindexnote()) (Indexnote *)
currentnote -> goup();

but I am not particularly happy using the cast. Presumably it could
also cause problems if I had further classes derived from Indexnote so
currentnote wasn't in fact merely an Indexnote.

The other approach would be to have the functions goup, godown and
gotonote as part of Note, but not doing anything in the base class.
Then I could do:

case WM_KEYDOWN:
if (wParam == VK_UP && currentnote -> isindexnote()) currentnote ->
goup();

but it seems a bit odd creating functions that have no real meaning.
The first approach is closer to what I have in my head.

I'm not very familiar with the way things work in windows so I can't
say what the best way to handle the key-events are but you could create
a method in the Note-class that takes the key-code as parameter and
then let the IndexNote-class override that method and add the needed
functionality.

If your note-program work the way I think it might be worth
investigating whether it would be possible for every note to "link" to
other notes like the index-note. So that a user can have a
note-hierarchy, which would mean that there would be no differance
between the index-note and other notes.
 
G

gw7rib

Erik said:
I'm not very familiar with the way things work in windows so I can't
say what the best way to handle the key-events are but you could create
a method in the Note-class that takes the key-code as parameter and
then let the IndexNote-class override that method and add the needed
functionality.

Thanks for your comments, Erik. I'm not sure that simply sending the
key-codes directly to the Note-class is the best approach - my feeling
is that my program ought to decide what it wants to happen when a key
is pressed and should tell the note to do that. Especially as in many
Windows programs there are often many ways of doing the same thing (key
presses, mouse clicks etc) and I feel this should be under the control
of the program. For example, instead of the program saying to the note
"The user has pressed up arrow - do what you think best about it" the
program ought to tell the note "do a go-up".
If your note-program work the way I think it might be worth
investigating whether it would be possible for every note to "link" to
other notes like the index-note. So that a user can have a
note-hierarchy, which would mean that there would be no differance
between the index-note and other notes.

I'm trying to make the program fairly simple to use. I wrote a program
before which contained items and directories of items, and you could
list the items in a directory, the items and subdirectories in a
directory, or all the items in or subsidiary to a directory - but it
was horrible to actually use so I didn't persist with it. With the
current system, you can create a note whose text is:

sort updated
show title updated

and pressing "enter" on this note will create an index note listing all
the notes in order of being updated. Another note might have the text:

select category fix
sort priority
show title

and pressing enter on this tells you all the things you need to fix, in
order of priority. So the index note is created to order - it has
different text (and different notes listed) depending on the text of
the creating note.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Thanks for your comments, Erik. I'm not sure that simply sending the
key-codes directly to the Note-class is the best approach - my feeling
is that my program ought to decide what it wants to happen when a key
is pressed and should tell the note to do that. Especially as in many
Windows programs there are often many ways of doing the same thing (key
presses, mouse clicks etc) and I feel this should be under the control
of the program. For example, instead of the program saying to the note
"The user has pressed up arrow - do what you think best about it" the
program ought to tell the note "do a go-up".

Then perhaps you should send an event, containing information about what
happened and let the Note decide what to do with it. I don't thing you
should take care of all input at one place and there decide what method
to call, it's better to tell the note what happened and let it decide.
 
N

Noah Roberts

Erik Wikström wrote:

Thanks for your comments, Erik. I'm not sure that simply sending the
key-codes directly to the Note-class is the best approach - my feeling
is that my program ought to decide what it wants to happen when a key
is pressed and should tell the note to do that. Especially as in many
Windows programs there are often many ways of doing the same thing (key
presses, mouse clicks etc) and I feel this should be under the control
of the program. For example, instead of the program saying to the note
"The user has pressed up arrow - do what you think best about it" the
program ought to tell the note "do a go-up".

You could look into boost::signals.
 
G

Grizlyk

case WM_KEYDOWN:
if (wParam == VK_UP && currentnote -> isindexnote()) currentnote ->
goup();

I hardly could understand what is each of "currentnote" and what they
do else,
but the piece of code looks like you need to eliminate "currentnote ->
isindexnote()"
and to do something like this:

case WM_KEYDOWN:
if (wParam == VK_UP )currentnote -> goup();

But if you have different types of "currentnote" (supported notes and
does not), you can change code, which is processing WM_KEYDOWN message,
for each type of "currentnote" makes own "message processing". For
"currentnote" without notes WM_KEYDOWN will be ignored.

The dividing can be done as if each logical visible rectangle on your
creen was developed as separated object with own "message processing"
member.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top