Article View: gnu.g++.bug
Article #152does "virtual" mean run-time computed function call ?
From: Christophe GROSJEAN
Date: Mon, 03 Aug 1992 16:42
Date: Mon, 03 Aug 1992 16:42
88 lines
4005 bytes
4005 bytes
-----------------------------------------------------------------------------
Christophe GROSJEAN Paris, le Mon 03 Aug 1992 a 16:38
-------------------
I naively thought that the adress of a virtual fonction call was computed a run-time (using look-up tables ?). However it seems i can't use the properties of virtual function outside inheriting classes. In order to create 'Smalltalk like' iterators for container classes, i wrote the the following code. In this example the Bug class is my container (it contains two integers : 1 and 2). The forall method is supposed to call the function f, that takes on parameter an element of my container, for every element
s of the container.
This code compile and works fine... except that, function::funcall being virtual, I was expecting the redefined function2::funcall to be called, not the base class one.
In fact in this example the "virtual" keyword seems to have no effect at all.
--------------------------------------------------------------------
#include <stdio.h>
#define DATA int
class function
{
public:
virtual void funcall(DATA i);
};
virtual void function::funcall(DATA i)
{
printf(" first definition = %d ", i);
}
class function2: public function
{
public:
virtual void funcall(DATA i) { printf(" redefined = %d ", i); }
};
class Bug
{
public:
void forall(class function f);
};
void Bug::forall(class function f)
{
f.funcall(1);
f.funcall(2);
}
main()
{
Bug b;
function2 f;
b.forall(f);
}
--------------------------------------------------------------------
Some of you may ask why I don't call function2::funcall instead of function::funcall in method Bug::forall. The idea is that Bug, is defined in a library provided to users and that "funcall" is user defined, every user that wants to call a fonction derive his own class from the base class "function".
What I would really like to do is to define class "function" as abstract. If i do so, my code won't even compile. The compiler says "Invalid call to abstract function".
It seems to be the same problem. I hope that it's a visibility problem and that I've missed something.
If it's a *language* problem as it occured with a previous problem i had with static data member, it will do no good for me.
What I need is either a fixed compiler (if it's a bug), or a way to workaround this problem if it isn't. I don't need iterator classes friend with my container classes. I've tried them, they solve my problem as long as my container classes are simple. But (too) many difficulty arise when i need to manipulate graphs or trees and most of recursive data structures (the problem is there are cases where it can't easealy save the context for the next iteration... may be with static variables inside member functi
on... I should try this).
Thanks for helping me.
--
_-----_
/ ) \
( ) )
\/ \ / \/
+---+ \ | / Christophe GROSJEAN
/ \ / \ \/|\/
/ / \ \O/ Equipe CAO&VLSI
+---+ \ \ /H\ Laboratoire MASI
\ \ +---+ / \ Institut Blaise Pascal
\ / /
\ / \ / Couloir 55-65, 2eme etage, Bureau 201
+---+ Universite Pierre et Marie Curie (P6)
4 place Jussieu, 75252 Paris Cedex 05
Tel: 44.27.30.43, Fax: 44.27.62.86
Telex: UPMCSIX200145F, e-mail: kriss@masi.ibp.fr
.
Technical Details
Message-ID:
<9208031644.AA20662@cao-vlsi.ibp.fr>Path:
arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!masi.ibp.fr!grosjeanShow Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!masi.ibp.fr!grosjean From: grosjean@masi.ibp.fr (Christophe GROSJEAN) Subject: does "virtual" mean run-time computed function call ? Message-ID: <9208031644.AA20662@cao-vlsi.ibp.fr> Sender: gnulists@ai.mit.edu Organization: GNUs Not Usenet Distribution: gnu Date: Mon, 3 Aug 1992 16:42:42 GMT Approved: bug-g++@prep.ai.mit.edu Lines: 88