This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
issue with polymorphism
- From: Daniel Franke <franke dot daniel at gmail dot com>
- To: Fortran List <fortran at gcc dot gnu dot org>
- Date: Sun, 06 May 2012 11:18:03 -0700 (PDT)
- Subject: issue with polymorphism
Hi all.
I'm trying to use the polymorphism features of F03 and got stuck with the
attached testcase - the Fortran and C++ versions should IMO be equivalent, but
the outcome is not:
$ gfortran hook.f90 && ./a.out
hook: a
$ g++ hook.cpp && ./a.out
hook b
[gcc version 4.6.1, Ubuntu system compiler]
I currently can't update to a 4.7 or latest svn as the build breaks due to
some multiarch setup issues recently introduced in Ubuntu/Debian.
Until I sorted out my system to compile myself, could somebody be so kind and
quickly run this through 4.7 and check if this difference still exists and if
yes, possibly comment if gfortran's behaviour is expected and correct?
Thanks
Daniel
MODULE m
TYPE :: a
CONTAINS
PROCEDURE :: worker => a_worker
PROCEDURE :: hook => a_hook
END TYPE
TYPE, extends(a) :: b
CONTAINS
PROCEDURE :: worker => b_worker
PROCEDURE :: hook => b_hook
END TYPE
CONTAINS
SUBROUTINE a_worker(this)
CLASS(a), INTENT(in) :: this
CALL this%hook()
END SUBROUTINE
SUBROUTINE a_hook(this)
CLASS(a), INTENT(in) :: this
print *, "hook: a" ! This is wrongly(?) called.
END SUBROUTINE
SUBROUTINE b_worker(this)
CLASS(b), INTENT(in) :: this
CALL this%a%worker()
END SUBROUTINE
SUBROUTINE b_hook(this)
CLASS(b), INTENT(in) :: this
print *, "hook: b"
END SUBROUTINE
END MODULE
USE m
TYPE(b) :: obj
CALL obj%worker()
END
#include <iostream>
class a {
public:
virtual void worker() {
hook();
}
virtual void hook() {
std::cout << "hook a" << std::endl;
}
};
class b : public a {
public:
void worker() {
a::worker();
}
virtual void hook() {
std::cout << "hook b" << std::endl; // This is correctly(?) called.
}
};
int main() {
b obj;
obj.worker();
return 0;
}