[g++ 2.95.1] problem with pointers to virtual methods

Jean-Francois MOINE d3moine@bst.bsf.alcatel.fr
Fri Nov 26 05:58:00 GMT 1999


/*
 * GNU g++ 2.95.1 - problem with pointers to virtual methods.
 *
 *	Bad code generation for calling a virtual method of a
 *	derivated class througth a pointer of the base class when
 *	this base class has no virtual methods.
 *
 * This code worked with g++ 2.8.1
 *
 * Generation:
 *	g++ thisfile.cc -o execfile
 */

//#define NOBUG	1	/* OK when defined, KO when undefined */

#include <stdio.h>

class base;
typedef void (base::*p_f)(void);

class base {
public:
	int	val;
	p_f	f;
	base(int i) { val = i; f = 0; };
	void call(void);
#ifdef NOBUG
	virtual void dum(void);
#endif
};

void base::call(void)
{
	(this->*f)();
}

#ifdef NOBUG
void base::dum(void) {}
#endif

class cl : public base {
public:
	cl() : base(1) {};
	virtual void v(void);
};

void cl::v(void)
{
	printf("val:%d\n", val);
}

int main(int argc, char *argv[])
{
	cl *inst;

	inst = new cl;
	inst->f = (p_f) &cl::v;
	inst->call();
	return 0;
}

-- 
Ken ar c'hentañ		** Breizh ha Linux atav ! **
		mailto:Jean-Francois.Moine@bst.bsf.alcatel.fr
Jef		(home mailto:moinejf@free.fr )
		http://moinejf.free.fr/




More information about the Gcc-bugs mailing list