7.1 vs 8.0: Inheritance and Events

R

rwf_20

Consider the following (/clr:blush:ldSyntax):

// myPanel.h
public __gc class myPanel {
public:
__delegate void PanelActivationHandler();
__event PanelActivationHandler* PanelActivated;

virtual void OnPanelActivated() {
std::cerr << "myPanel::OnPanelActivated()\n";
}

};

public __gc class derivedPanel : public myPanel {
public:
virtual void OnPanelActivated() {
std::cerr << "derivedPanel::OnPanelActivated()\n";
}

void generateEvent() {
PanelActivated();
}
};

// end myPanel.h

// test.cpp
int main() {
derivedPanel* p = new derivedPanel();

p->add_PanelActivated(new myPanel::panelActivationHandler(p,
&myPanel::OnPanelActivated));

p->generateEvent();

return 0;
}

// end test.cpp

In VisualStudio 2003, this code calls the base class event handler:
"myPanel::OnPanelActivated()"

In VisualStudio 2005, this code calls the derived class event handler:
"derivedPanel::OnPanelActivated()"

So, my questions are:
Which one should be called? I would think that the 2005 behavior is
correct; however, the static scoping of the function pointer
(&myPanel::OnPanelActivated) concerns me.

Is there a way to make this work in both versions? Keep in mind that,
in my real application, the code calling add_PanelActivated knows only
the base type.

Thanks in advance,
Ryan
 
C

Carl Daniel [VC++ MVP]

rwf_20 said:
Consider the following (/clr:blush:ldSyntax):

[code snipped]
In VisualStudio 2003, this code calls the base class event handler:
"myPanel::OnPanelActivated()"

This is clearly a bug.
In VisualStudio 2005, this code calls the derived class event handler:
"derivedPanel::OnPanelActivated()"

This is correct.
So, my questions are:
Which one should be called? I would think that the 2005 behavior is
correct; however, the static scoping of the function pointer
(&myPanel::OnPanelActivated) concerns me.

I agree.
Is there a way to make this work in both versions? Keep in mind that,
in my real application, the code calling add_PanelActivated knows only
the base type.

I assume that what you want is the 2005 behavior in the 2003 build, right?
In that case, I would think that you'd have to do something like provide a
pure virtual function in your base class that every derived class must
override. Form the pointers to members from within the implementations of
that virtual function, and call that new virtual function from your
add_PanelActivated function rather than composing the pointer to member
directly at that point.

-cd
 

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

Top