richard@pantor.UUCP (Richard Sargent) (02/24/90)
A question came up recently here as to what is correct code for a C++ 2.0 program. We have a derived tree of classes of graphical objects, with the base class defining a 'virtual void draw( some *obj )'. In one derived class, a way down the tree of classes, we have a class in which we want to have a function 'void draw( void )'. That is, we want a function for that particular class which takes no argument. Attempting to do this the obvious way results in a type mismatch. Q: is it necessary that the base class define 'virtual void draw( void )' to overload it, even though the only class to use it is way down the tree of classes? If we do this then we get an error message on the derived classes which define 'void draw( some *obj )', namely "sorry, not implemented: virtual derclass::draw() overloaded in base class but not in derived class". So, does this mean that we have to specify the overloading of the 'void draw( void )' in all derived classes? Q: exactly what do we need to do to make this work? If anyone has done something like this in 2.0, we certainly would appreciate hearing how? Thanks for your help. Richard Sargent Internet: richard@pantor.UUCP Systems Analyst UUCP: ...!mnetor!becker!pantor!richard
richard@pantor.UUCP (Richard Sargent) (03/20/90)
(I apologize if this has made it out before. I have not seen any indication that it has done so, hence the repost.) A question came up recently here as to what is correct code for a C++ 2.0 program. We have a derived tree of classes of graphical objects, with the base class defining a 'virtual void draw( some *obj )'. In one derived class, a way down the tree of classes, we have a class in which we want to have a function 'void draw( void )'. That is, we want a function for that particular class which takes no argument. Attempting to do this the obvious way results in a type mismatch. Q: is it necessary that the base class define 'virtual void draw( void )' to overload it, even though the only class to use it is way down the tree of classes? If we do this then we get an error message on the derived classes which define 'void draw( some *obj )', namely "sorry, not implemented: virtual derclass::draw() overloaded in base class but not in derived class". So, does this mean that we have to specify the overloading of the 'void draw( void )' in all derived classes? Q: exactly what do we need to do to make this work? If anyone has done something like this in 2.0, we certainly would appreciate hearing how? Thanks for your help. Richard Sargent Internet: richard@pantor.UUCP Systems Analyst UUCP: ...!mnetor!becker!pantor!richard
ark@alice.UUCP (Andrew Koenig) (03/21/90)
In article <58.UUL1.3#5109@pantor.UUCP>, richard@pantor.UUCP (Richard Sargent) writes: > We have a derived tree of classes of graphical objects, with the > base class defining a 'virtual void draw( some *obj )'. In one > derived class, a way down the tree of classes, we have a class > in which we want to have a function 'void draw( void )'. That is, > we want a function for that particular class which takes no > argument. > Attempting to do this the obvious way results in a type mismatch. If you do something like this: class Base { public: virtual void draw(some*); }; class Derived: public Base { public: virtual void draw(); }; you haven't exactly done anything wrong, but you probably didn't do what you expected. Unless the argument types match exactly in the base and derived classes, you have defined two totally different functions. That is, if you say Base* bp = new Derived; bp->draw(); the compiler will complain because Base::draw(void) isn't defined. If instead you say bp->draw(sp); where `sp' is a some*, that's fine -- but you will call Base::draw(some*) and not Defined::draw(void). > Q: is it necessary that the base class define 'virtual void draw( void )' > to overload it, even though the only class to use it is way down > the tree of classes? > If we do this then we get an error message on the derived classes > which define 'void draw( some *obj )', namely "sorry, not implemented: > virtual derclass::draw() overloaded in base class but not in derived > class". So, does this mean that we have to specify the overloading > of the 'void draw( void )' in all derived classes? If you are really getting a `sorry not implemented' message instead of a warning, that strongly suggests that you are not doing exactly what you illustrated here. Perhaps you could post a complete example? Preferably a small one. -- --Andrew Koenig ark@europa.att.com