🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Bala SWAMINATHAN Mon, 02 Nov 1992 18:24
templates and function pointers
#25
Author: Bala SWAMINATHAN
Date: Mon, 02 Nov 1992 18:24
44 lines
1659 bytes
I have the following template class which has a function ptr variable
"print_item"

I'd like the user to give a function which prints each item. If the
user does not set the value of the print_item variable then I want
the p_item function I define within class.. How to do this?

I did in thefollowing ways and gnu compiler refuses to compile:

template<class T>
class A {
	T	a[100];
	void	p_item(T t) { printf(" %2d",t); }
	void	(*print_item)(T);
public:
	A() { print_item = this->p_item; } // -- says incompatible assignment --
	void	print() { // print all elements of array using "print_item"}
};

OR


template<class T>
void	p_item(T t) {printf(" %2d",t); }

class A {
	T	a[100];
	void	(*print_item)(T);
public:
	A() { print_item = p_item; }
	void	print() { // print all elements of array using "print_item"}
};

-- In this case there is some compiler error in g++ which looks like
cp-class.c:3977: failed assertion `TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL'
/pkg/gnu/bin/gcc: Internal compiler error: program cc1plus got fatal signal 6
*** Error code 1


What am I doing wrong? Or is it not possible to be
too generic with templates at all? Any pointers would be
appreciated. Thank you.

S_Bala
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads