__PureVirtualCalled

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • What Ever

    __PureVirtualCalled

    Hi,

    I got __PureVirtualCa lled exception when I have defined a public
    function in the base class, and its implementation calls number of
    "pure virtual functions", which are overridden in the derived class.

    For example

    class Base
    {
    public:
    void foo();
    virtual ~Base();
    private:
    virtual void pure1() = 0;
    virtual void pure2() = 0;
    virtual void pure3() = 0;
    };

    void Base::foo()
    {
    pure1();
    pure2();
    pure3();
    }

    class Derived : public Base
    {
    private:
    void pure1();
    void pure2();
    void pure3();
    };

    void Derived::pure1( )
    {
    // do something
    }

    void Derived::pure2( )
    {
    // do something
    }

    void Derived::pure3( )
    {
    // do something
    }

    Can someone please explain to me why I would be getting
    __PureVirtualCa lled exception? Can't I call a pure virtual function
    from the base class. BTW, as you have seen I did not provide any
    implementation for the pure virtual in base class.

    I'm using HP's aCC compiler.

    Thanks in advance.
  • Victor Bazarov

    #2
    Re: __PureVirtualCa lled

    "What Ever" <my_google2001@ yahoo.com> wrote...[color=blue]
    > I got __PureVirtualCa lled exception when I have defined a public
    > function in the base class, and its implementation calls number of
    > "pure virtual functions", which are overridden in the derived class.
    >
    > For example
    >
    > class Base
    > {
    > public:
    > void foo();
    > virtual ~Base();
    > private:
    > virtual void pure1() = 0;
    > virtual void pure2() = 0;
    > virtual void pure3() = 0;
    > };
    >
    > void Base::foo()
    > {
    > pure1();
    > pure2();
    > pure3();
    > }
    >
    > class Derived : public Base
    > {
    > private:
    > void pure1();
    > void pure2();
    > void pure3();
    > };
    >
    > void Derived::pure1( )
    > {
    > // do something
    > }
    >
    > void Derived::pure2( )
    > {
    > // do something
    > }
    >
    > void Derived::pure3( )
    > {
    > // do something
    > }
    >
    > Can someone please explain to me why I would be getting
    > __PureVirtualCa lled exception?[/color]

    Cannot tell. Where do you call 'foo'? From the c-tor?
    [color=blue]
    > Can't I call a pure virtual function
    > from the base class.[/color]

    You can. The result depends on where the call originates.
    If it originates from a constructor or the destructor, the
    result is undefined.
    [color=blue]
    > BTW, as you have seen I did not provide any
    > implementation for the pure virtual in base class.[/color]

    You can call a pure virtual function from the constructor, but
    in that case the call will _not_ be polymorphic. The derived
    class object, which overrides those functions, has not been
    constructed yet, so the call is resolved statically. If you do
    not have a definition (body) for the pure virtual function you
    are trying to call from a c-tor, the behaviour is undefined.

    It's basically like dereferencing a null pointer.

    This is one of the most dramatic differences between C++ and
    Java, for example. The latter allows calling virtual functions
    from constructor and does that polymorphically . So, you end up
    inside a member of the derived class when the derived object
    hasn't even begun constructing! Yecch...

    Victor


    Comment

    • What Ever

      #3
      Re: __PureVirtualCa lled

      my_google2001@y ahoo.com (What Ever) wrote in message news:<feb43be.0 308131651.47d82 b9a@posting.goo gle.com>...[color=blue]
      > Hi,
      >
      > I got __PureVirtualCa lled exception when I have defined a public
      > function in the base class, and its implementation calls number of
      > "pure virtual functions", which are overridden in the derived class.
      >
      > For example
      >
      > class Base
      > {
      > public:
      > void foo();
      > virtual ~Base();
      > private:
      > virtual void pure1() = 0;
      > virtual void pure2() = 0;
      > virtual void pure3() = 0;
      > };
      >
      > void Base::foo()
      > {
      > pure1();
      > pure2();
      > pure3();
      > }
      >
      > class Derived : public Base
      > {
      > private:
      > void pure1();
      > void pure2();
      > void pure3();
      > };
      >
      > void Derived::pure1( )
      > {
      > // do something
      > }
      >
      > void Derived::pure2( )
      > {
      > // do something
      > }
      >
      > void Derived::pure3( )
      > {
      > // do something
      > }
      >
      > Can someone please explain to me why I would be getting
      > __PureVirtualCa lled exception? Can't I call a pure virtual function
      > from the base class. BTW, as you have seen I did not provide any
      > implementation for the pure virtual in base class.
      >
      > I'm using HP's aCC compiler.
      >
      > Thanks in advance.[/color]


      Victor, and David thanks for your time. No, I am not calling a virtual
      function from the constructor. The problem was in my makefile, I
      changed signatures of some of the functions, and that did not force
      the recompilation of certain source files. I am guessing that caused
      the problem, a complete re-build fixed it.

      Thanks again.

      Comment

      Working...