giving an istance method to a superclass's constructor

Hi, it is my first post here~ :). I find this website very useful and I am often using it to solve my c++ problems!
Anyway I was not able to find how to pass an instance method to a constructor of the superclass.
Below is the code I am working on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Base{
public:
	int (*funky) (void);

	Base(int (*funkyName) (void)){
		this->funky = funkyName;
	}

	virtual void callfunky(){
		cout << "value: " << funky() << '\n';
	}
};

class Derived: public Base{
public:

	int myFunkyTranky(){
			return -1;
	}

	Derived(): Base(myFunkyTranky){

	}


};


The problem is in the constructor of the class 'Derived', where I try to give as argument an instance method of the same class.
The compiler generates the following error:
no matching function for call to 'Base::Base(<unresolved overloaded function type>)'
Is there any way to achieve this functionality in C++?

Thanks in advance
Bye
- Dean
Either the function needs to be static or you need a pointer to a member function.

Anyway, you're trying to emulate the functionality of virtual functions here.
This is how it should be done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Base {
public:
	virtual int funky()=0;

	Base() {}

	void callfunky() {cout << "value: " << funky() << '\n';}
};

class Derived: public Base {
public:

	virtual int funky() {return -1;}

	Derived() {}

};
This is can be resolved by using a templated base class.

http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

Look at the polymorphic copy construction example. That's probably closest to what you need.

Topic archived. No new replies allowed.