[GH-ISSUE #28] Missing capture in gui tutorial Gui09 #13

Closed
opened 2026-05-05 03:34:05 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @mingodad on GitHub (Jan 5, 2021).
Original GitHub issue: https://github.com/ultimatepp/ultimatepp/issues/28

While following the gui tutorial I found a mistake in Gui09 project, the lambda was capturing the i variable by reference and should be by copy otherwise we get always a value of 10 the las assigned value in the for loop.

@@ -10,11 +10,11 @@ struct MyAppWindow : TopWindow {
 
 	virtual void RightDown(Point, dword) override {
 		int result = Null;
 		MenuBar menu;
 		for(int i = 0; i < 10; i++)
-			menu.Add(AsString(i), [&] { result = i; });
+			menu.Add(AsString(i), [&, i] { result = i; });
 		menu.Separator();
 		menu.Add("Exit", [=] { Exit(); });
 		menu.Execute();
 		if(!IsNull(result))
 			PromptOK("You have selected " + AsString((int)result));
Originally created by @mingodad on GitHub (Jan 5, 2021). Original GitHub issue: https://github.com/ultimatepp/ultimatepp/issues/28 While following the gui tutorial I found a mistake in Gui09 project, the lambda was capturing the `i` variable by reference and should be by copy otherwise we get always a value of `10` the las assigned value in the for loop. ``` @@ -10,11 +10,11 @@ struct MyAppWindow : TopWindow { virtual void RightDown(Point, dword) override { int result = Null; MenuBar menu; for(int i = 0; i < 10; i++) - menu.Add(AsString(i), [&] { result = i; }); + menu.Add(AsString(i), [&, i] { result = i; }); menu.Separator(); menu.Add("Exit", [=] { Exit(); }); menu.Execute(); if(!IsNull(result)) PromptOK("You have selected " + AsString((int)result)); ```
Author
Owner

@klugier commented on GitHub (Jan 5, 2021):

Should be fixed in both places (code and documentation). Thanks for report!

<!-- gh-comment-id:754585210 --> @klugier commented on GitHub (Jan 5, 2021): Should be fixed in both places (code and documentation). Thanks for report!
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/ultimatepp#13
No description provided.