Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!microsoft!ilanc From: ilanc@microsoft.UUCP (Ilan CARON) Newsgroups: comp.lang.c++ Subject: Re: Multiple Inheritance Problem Message-ID: <72229@microsoft.UUCP> Date: 8 May 91 15:08:29 GMT References: <74462@brunix.UUCP> Reply-To: ilanc@microsoft.UUCP (Ilan CARON) Organization: Microsoft Corp., Redmond WA Lines: 95 C++ 2.1 now does for you the service of not requiring you to redeclare your pure virtual functions in derived classes (i.e. it automatically redeclares them for you as pure virtual). (2.0 made you redeclare them explicitly). |class Object { |public: | | Object(); | ~Object(); | | virtual void Draw() = 0; | | virtual void Update() = 0; | | virtual Object* Dup() = 0; | |} | |class Line: public virtual Object { |public: | | Line(); | ~Line(); | | void Draw(); // The 2.1 compiler effectively adds these decls. virtual void Update() = 0; virtual Object* Dup() = 0; | |} | |class Dependent: public virtual Object { |public: | | Dependent(); | ~Dependent(); | | void Update(); // The 2.1 compiler effectively adds these decls. virtual void Draw() = 0; virtual Object* Dup() = 0; | |} | | |class DependentLine: public Line, public Dependent { ^^^^ You forgot this |public: | | DependentLine(); | ~DependentLine(); | | Object *Dup() { return( (new DependentLine) ); }; // error here | |} | |Sun CC version 2.1 gives me an error on the line |marked above, saying | |"./ChildrenObjs.H", line 30: error: `new' of abstract class DependentLine | |What am I doing wrong? In order for a class to be concrete (non-abstract), so that you can create instances of it with operator 'new', it must be the case that it has no pure virtual functions. In above hierarchy, Object, Dependent and Line are clearly abstract classes. The question is: why is DependentLine also abstract? Well, DependentLine only explicitly overrides Dup(). What about Draw() and Update()? From one base class it inherits a pure virtual version and from the other an implementation. In any event, in the derived class it is not clear which version to "use". You might think that it should prefer the implementation as opposed to the pure virtual definition -- but on reflection that doesn't make much sense. Consider this fragment: Line *pLine = new DependentLine; pLine->Update(); // ??? use Dependent::Update() ??? So, in conclusion, DependentLine is an abstract class because it doesn't provide an explicit implementation for Draw() and Update(). Hope this makes sense. --ilan caron (uunet!microsoft!ilanc)