When virtual is not working

I have a strange error in my C ++ classes at the moment. I have an ActiveX wrapper class (as part of wxWidgets) to which I have added a new virtual function. I have another class that inherits from ActiveX (wxIEHtmlWin), however the ActiveX class always calls its own function instead of the one in wxIEHtmlWin that overrides it.

I can’t understand why this is happening. I made the function pure virtual, and now the program crashes when it makes a function call, but compiles otherwise. Is there a way to disable virtual functions or have I found an error in Visual Studio?

ActiveX class

protected: virtual FrameSite* getNewFrameSite()=0; 

Class wxIEHtmlWin

 class wxIEHtmlWin : public wxActiveX { protected: FrameSite* getNewFrameSite(); } FrameSite* wxIEHtmlWin::getNewFrameSite() { return new gcFrameSite(this); } 

Edit: I added another test function (returns int) and still clamps.

Link to the code: http://lodle.net/public/iebrowser.rar

Edit:

OK, thanks to the answer below, I got it to work. What I did was create an activex class in two parts (for example, the one suggested), however in wxIEHtmlWin I named the second part of the constructor code. For example:

 wxIEHtmlWin::wxIEHtmlWin(wxWindow * parent, wxWindowID id, const wxPoint& pos,const wxSize& size,long style, const wxString& name) : wxActiveX() { wxActiveX::Create(parent, PROGID, id, pos, size, style, name); SetupBrowser(); } 

Now I know why wxWidgets supports the construction of two parts.

+3
c ++ visual-studio virtual wxwidgets
Dec 24 '08 at 8:04
source share
2 answers

You call the virtual method from the constructor of the class (via another call). This will call the method in the current class, since the subclass is not yet built. The fix is ​​to use the init () method and call it after the class is built.

Something like that:

 class wxActivex { wxActivex() {} virtual void init() { getNewFrame(); } }; // in the code that uses these classes: wxActivex *activex = new IEHtmlFrame(); activex->init(); 
+9
Dec 24 '08 at 8:23
source share

A more β€œflipped out” version of this question can be found here . But, in short, the underlying object is not yet an instance of the derived type, so you cannot call any overloaded functions on the derived object.

+1
03 Feb '09 at 13:35
source share



All Articles