This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C++ question: MI vs. pure virtuals



On Fri, 31 Dec 1999, Chip Salzenberg wrote:

> I've studied the Standard but I can't seem to find an answer to this
> question: Are inherited *non-virtual* functions supposed to provide
> implementations for pure virtuals?
> 
> For example, given this:
> 
> 	class A { virtual void f() = 0; };  // PURE VIRTUAL
> 	class B { void f() {} };            // NON-VIRTUAL
>         class C {};                         // ??
> 
> Is class C abstract like A, or not?

The way you wrote it, it is not abstract; it does not contain any pure
  virtual functions.

However, given your question, I think you accidently left something out of
  your sample code. (That is, you mention 'inherited non-virtual' and you 
  have MI in your subject line, but none of those classes contain any
  inheritance of any kind the way you declared them.)

So I am going to assume you meant to ask about this:

  struct A {virtual void f()=0;};
  struct B {void f() {}};
  struct C :public A,public B {};

  int main()
  {
    C c; //Can C be instantiated? (No; it is abstrct.)
    c.f(); //What function does this call? (Niether; it is ambigous.)
  }


Since A is not a base class of B, B::f() does not overide (does not
  implement) A::f(). (See sections 10.2, p2 (for name lookup), 10.3 p2,
  (for overriding of virtual functions) 10.4 (abstract base classes), and 
  pay particular attention to all of the examples in these sections.)

Since B::f() does not provide an implementation for A::f(), C still
  inherits a pure virtual function and is therefore abstract; 'C c' is an
  error as C cannot be instantiated.

There are two distinct functions called f() : A::f(), and B::f(), and
  'c.f()' provides no hints as to which function was called, and is
  therefor ambigous.

by the way, gcc 2.95.2 gives:

MI_pure_virtual_vs_non_virtual.cc: In function `int main()':
MI_pure_virtual_vs_non_virtual.cc:7: cannot declare variable `c' to be of
type `C'
MI_pure_virtual_vs_non_virtual.cc:7:   since the following virtual
functions are abstract:
MI_pure_virtual_vs_non_virtual.cc:1:    void A::f()
MI_pure_virtual_vs_non_virtual.cc:8: request for member `f' is ambiguous
MI_pure_virtual_vs_non_virtual.cc:2: candidates are: void B::f()
MI_pure_virtual_vs_non_virtual.cc:1:                 void A::f()

which is not gospel, but it correlates with my understanding of the
  standard.

I apologize if I answered the wrong question.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]