Page 1 of 1

C++ again...

Posted: Wed Jan 29, 2003 3:27 pm
by whyme_t
I'm having some problems with inheritance, overloading inherited functions, and virtual functions.

Code: Select all

FooBase foo1 = new FooBase() ;
foo1.bar() ; //this works ok
FooSub foo2 = new FooSub() ;
foo2.bar() ; //this works ok
FooBase foo3 = new FooSub() ;
foo3.bar() ; //shouldn't this call FooSub::bar() instead
                   //of  FooBase::bar() ?
Any form of virtual (virtual, and pure virtual) function (you need a virtual deconstructor in any class you will inherit from), causes an invalid op code exception.

BTW, if your using djgpp gcc. Use the support code on my site, and link libsupcxx.a between your kernel stub, and object files, and code for vtable will be enabled for inheritance to work correctly. You will get one undefined reference to __cxa_pure_virtual. From looking around the web for info, I've defined it as so,

Code: Select all

extern "C" void __cxa_pure_virtual (void)
{
   k_panic("__cxa_pure_virtual called!") ;
}
But I need to research the actually use of this function.

Any ideas how I can find why virtual functions causes an invalid op code? I've found this out by having a default trap handler which prints out which exception had occurred.

Re:C++ again...

Posted: Wed Jan 29, 2003 3:46 pm
by pskyboy
Hey whyme_t

Your braver then me i decided i wasn't going to use virtual inheritance as it has quite a large cost on performance in as your finding out is an arse to implement in an OS environment.

What i am doing is implementing the functions in every object i know this costs a bit more on memory but it is easier and faster. All i do is set up a grr i can't rember what there called and can't find it in my book ill just do the source and hopefully you'll know.

Code: Select all

UINT SomeFunction() = 0;

Re:C++ again...

Posted: Wed Jan 29, 2003 4:07 pm
by whyme_t
Is it something like, a Pure specifier? Because a pure virtual functions looks like,

Code: Select all

virtual UNIT SomeFunction() = 0;
EDIT :- Does polymorphism work for you with this method? I can't get it to work. It always calls the base classes methods, not that of the sub class passed in.

Re:C++ again...

Posted: Wed Jan 29, 2003 5:17 pm
by Tim
It will do if the virtual method table is correct. The only place that the vtable won't be correct by design is inside a base class's constructor (where the vtable for the base class is used).

Anything else means that something is broken, although there's not much that can go wrong with something as basic as virtual method calls.