This is the mail archive of the gcc@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]
Other format: [Raw text]

Calling a pure virtual function


Hi all,

I was expecting the following code snippet to work, so am I doing
something wrong, or is there an issue with GCC?  I was under the
impression that this is allowed, according to
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.1

It seems like GCC initially allows it as it starts to compile okay, but
then I get an undefined reference error from the linker (because it
seems to be actually calling Base::number(), which obviously won't work
as it's a pure virtual function.)

I was only able to try this with GCC 3.2.3 (i486 target) and 3.3.6
(djgpp/msdos target) and both versions give the same error.

Thanks,
Adam.

------------------------------------------------------

#include <iostream>

using namespace std;

class Base {
  public:
    Base()
    {
      cout << "This is class " << this->number();
    }

    virtual int number() = 0;
};

class One: virtual public Base {
  public:
    int number()
    {
      return 1;
    }
};

int main(void)
{

  // Correctly fails stating Base is abstract
//  Base *b = new Base();

  // Won't compile giving undefined reference to Base::number()
  One *o = new One();

  return 0;
}


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