Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Callbacks and Timers » "Function" question
"Function" question [message #47714] Thu, 09 March 2017 14:19 Go to next message
koldo is currently offline  koldo
Messages: 2769
Registered: August 2008
Location: Basque Country
Senior Veteran
Hello all

I wanted to assign a Function from a subclass but compiler complains with error:

'&': illegal operation on bound member function expression

What have I to do?
class FuncSource {
protected:
	Function <double(double)> function;

public:
	FuncSource(Function <double(double)> function) : function(function) {}
	double f(double x)	{return function(x);}
};

class MySource : public FuncSource {
public:
	MySource(double data) : FuncSource(&Calc), data(data) {}  // COMPILER ERROR
	
	double Calc(double x) {return x + data/10;}
	
private:
	double data;
};


Best regards
Koldo

[Updated on: Thu, 09 March 2017 14:19]

Report message to a moderator

Re: "Function" question [message #47715 is a reply to message #47714] Thu, 09 March 2017 17:52 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 2769
Registered: August 2008
Location: Basque Country
Senior Veteran
Replacing

MySource(double data) : FuncSource(&Calc), data(data) {}  // COMPILER ERROR

with
MySource(double data) : FuncSource([=](double x) {return Calc(x);}), data(data) {}
it compiles, but at run time when Calc() have to be used, it has been destroyed.


Best regards
Koldo
Re: "Function" question [message #47716 is a reply to message #47715] Thu, 09 March 2017 18:33 Go to previous message
koldo is currently offline  koldo
Messages: 2769
Registered: August 2008
Location: Basque Country
Senior Veteran
Well, it is solved
void Init(double data) {
	this->data = data;
	function = [=](double x) {return Calc(x);};
}

Now MySource is inside main model, so it does not get destroyed after initialization Rolling Eyes.


Best regards
Koldo
Previous Topic: Event.execute
Next Topic: PostCallback in constructor
Goto Forum:
  


Current Time: Wed Oct 18 16:39:22 CEST 2017

Total time taken to generate the page: 0.00963 seconds