[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
virtual function in destructor
From: |
Steffen Jahn |
Subject: |
virtual function in destructor |
Date: |
17 Apr 2001 15:30:39 GMT |
Hi,
another strange behavior is the handling of virtual
functions in desctructors. Please take a look at the
examples below.
The first example causes no problems.
struct A
{
~A(void) { f() ; }
virtual void f(void) = 0 ;
} ;
struct B : public A
{
virtual void f(void) { }
} ;
void main() { B b ; }
The second example, which is the same as the first one,
does only define the destructor outside the class scope.
struct A
{
~A(void) ;
virtual void f(void) = 0 ;
} ;
A::~A(void) { f() ; }
struct B : public A
{
virtual void f(void) { }
} ;
void main() { B b ; }
And g++ (linker) complains:
/usr/bin/ld:
Unresolved:
A::f(void)
collect2: ld returned 1 exit status
As a work around, you have to use virtual function
only in the class scope (inline) in destructors.
regards
Steffen
btw:
% g++ -v
Reading specs from .../alphaev56-dec-osf4.0d/2.95.3/specs
gcc version 2.95.3 20010315 (release)
--
address@hidden
- virtual function in destructor,
Steffen Jahn <=