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 » THISBACK1 works well, how can i make it with lambda
THISBACK1 works well, how can i make it with lambda [message #48319] Thu, 22 June 2017 03:41 Go to next message
MuratAKAR is currently offline  MuratAKAR
Messages: 3
Registered: June 2017
Junior Member
First of all, I would like to thank all the developers.

I have a form with 100 button. When i click a button, its Label change to "X". I made it with THISBACK1. How can i make it with lambda.
index.php?t=getfile&id=5312&private=0

class AnaForm: public WithAnaFormGUI<TopWindow>
{
	Button butonlar[100];
	void btnDuzenle(Button*);
	typedef AnaForm CLASSNAME;
public:
	AnaForm();
};

void AnaForm::btnDuzenle(Button* btnDuzenle)
{
	btnDuzenle->SetLabel("X");
}

AnaForm::AnaForm()
{
	CtrlLayout(*this, "Deneme");

	for(int i=0;i<100;i++)
	{
		butonlar[i].LeftPosZ(i%10*30,28).TopPosZ(i/10*30,28);
		butonlar[i].SetLabel(String()<<i);
		Add(butonlar[i]);
		
		butonlar[i]<<THISBACK1(btnDuzenle, &butonlar[i]); //this works well

		butonlar[i]<<[](){ //How can i make it with lambda
			
		};
	}
}


Re: THISBACK1 works well, how can i make it with lambda [message #48322 is a reply to message #48319] Thu, 22 June 2017 11:36 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Hello,

You are actually capturing nothing ("[]") while you really need to capture variables either by reference ("&") or by copy ("=").
(Note that in both cases (whether you use [&] or [=]) the "this" pointer will be automatically captured, so you'll have access to AnaForm class' members such as butonlar[] array.):

Also, you really don't need a (empty) parameter list here. It can be omitted.

butonlar[i]<<[&]{ btnDuzenle(&butonlar[i]); }; // this should work, since butonlar[] is a member of AnaForm class.
butonlar[i]<<[=]{ btnDuzenle(&butonlar[i]); }; // this should work too.

// And you can also get rid of the member function AnaForm::btnDuzenle()

butonlar[i]<<[=]{ butonlar[i].SetLabel("X"); }; // this is IMO a more elegant solution in such  simple cases (given your example code).
 



Best regards,
Oblivion


[Updated on: Thu, 22 June 2017 11:50]

Report message to a moderator

Re: THISBACK1 works well, how can i make it with lambda [message #48326 is a reply to message #48322] Thu, 22 June 2017 23:13 Go to previous messageGo to next message
MuratAKAR is currently offline  MuratAKAR
Messages: 3
Registered: June 2017
Junior Member
I've tried all of them before. But they did not work. Because variable i has value 100 after for loop so when a button pressed lambda function looks butonlar[100] and crash.

[Updated on: Fri, 23 June 2017 03:21]

Report message to a moderator

Re: THISBACK1 works well, how can i make it with lambda [message #48328 is a reply to message #48326] Fri, 23 June 2017 08:29 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Hello Murat,

It doesn't seem to be a lambda problem then. Maybe you overlooked something in the loop?
If you could provide a test-case, I may be able to help.

[Turkish:]

Öncelikle, hoş geldin!
Sorun lambda fonksyion gibi görünmüyor.
Bir sonraki mesajına aynı hatayı veren derlenebilir küçük bir örnek ekleyebilirsen yardımcı olabilirm.



Best regards,
Oblivion


[Updated on: Fri, 23 June 2017 08:39]

Report message to a moderator

Re: THISBACK1 works well, how can i make it with lambda [message #48331 is a reply to message #48328] Fri, 23 June 2017 23:52 Go to previous messageGo to next message
MuratAKAR is currently offline  MuratAKAR
Messages: 3
Registered: June 2017
Junior Member
#include <CtrlLib/CtrlLib.h>

using namespace Upp;

class AnaForm: public TopWindow
{
	Button butonlar[100];
	void btnDuzenle(Button*);
	typedef AnaForm CLASSNAME;
public:
	AnaForm();
};

void AnaForm::btnDuzenle(Button* btnDuzenle)
{
	btnDuzenle->SetLabel("X");
}

AnaForm::AnaForm()
{
	SetRect(100,100,320,345);
	for(int i=0;i<100;i++)
	{
		butonlar[i].LeftPosZ(i%10*30,28).TopPosZ(i/10*30,28);
		butonlar[i].SetLabel(String()<<i);
		Add(butonlar[i]);
		//butonlar[i]<<THISBACK1(btnDuzenle, &butonlar[i]); Works well
			
	
		//butonlar[i]<<[&]{ btnDuzenle(&butonlar[i]); }; Not Works
		
		butonlar[i]<<[=]{ btnDuzenle(&butonlar[i]); }; //Works well
		butonlar[i]<<[=]{ butonlar[i].SetLabel("X"); };//Works well
		
	}
}

GUI_APP_MAIN
{
	SetLanguage(GetSystemLNG());
	AnaForm frm;
	frm.Run();
}


butonlar[i]<<[=]{ btnDuzenle(&butonlar[i]); }; //Works well
butonlar[i]<<[=]{ butonlar[i].SetLabel("X"); };//Works well

Sory, I have not tried both. Problem solved. Thanks..

Turkish:
Teşekkür ederim. Baya kod denemiştim ama ikisini denememişim. Sağolasın. İyi bayramlar. Smile
Re: THISBACK1 works well, how can i make it with lambda [message #48336 is a reply to message #48322] Sat, 24 June 2017 14:58 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Oblivion wrote on Thu, 22 June 2017 11:36

butonlar[i]<<[&]{ btnDuzenle(&butonlar[i]); }; // this should work, since butonlar[] is a member of AnaForm class.



Careful here! Do you really want to capture i by reference?

Personally, I believe that the use [&] should be restrict only for cases where lambda is used as 'local function' (not stored anywehere).
Re: THISBACK1 works well, how can i make it with lambda [message #48351 is a reply to message #48336] Sun, 25 June 2017 21:20 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Hello Mirek,

Of course you are right. I simply pointed the possible ways.
On storing references and pointers to callbacks, I asked a question here:
http://www.ultimatepp.org/forums/index.php?t=msg&goto=48 350&#msg_48350
If you have time, I'd be grateful If you could share your thoughts on the matter. (I need to solve this problem before I publish the SSH package for U++)

Best regards,

Oblivion


Previous Topic: callbacks freeze gui
Next Topic: About storing references and pointers to callbacks.
Goto Forum:
  


Current Time: Thu Mar 28 22:43:20 CET 2024

Total time taken to generate the page: 0.01915 seconds