So the few questions are:
-How would you advise to make the access specifier of print private and not public ?
The rest of the questions focus on a really specific function that i pinpointed that it causes all of the problems with adding submenus:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void CompositeMenu::addChild(ContainerComponent& child) //taking const reference causes c2440 error on line 31
{
if (std::find_if(m_children.begin(),
m_children.end(),
[&child](const ContainerComponent* cmp)
{return cmp->getName() == child.getName(); }) == m_children.end()) //is it possible to still compare function addresses ?
{
if (std::is_same<decltype(child), CompositeMenu>::value)
{
m_children.emplace_back(&Action(child.getName(), std::bind(&ContainerComponent::call, &child)));
}//end of if
else m_children.emplace_back(&child);
if (m_enabledBoxedMenu) updateLengths(child.getName(), boxedLengthPadding);
else updateLengths(child.getName(), bareLengthPadding);
}//end of if
else std::cout << "An Action with this name already exists.\n";
}
|
-How should i get past the c2440 error and still pass by const reference ?
-From what iv'e read i cant get a member function address that has the "this" pointer but i still want to be able to check if an Action with the same m_action exists is it possible ?
-
m_children.emplace_back(&Action(child.getName(), std::bind(&ContainerComponent::call, &child)));
In my opinion is the root of the problem, i know its an awful idea to pass a reference to a temporary object but i cant seem to find a way around, i tried using unique_ptr and shared_ptr but they cause even more errors which are thrown not from the code itself but the algorithm library, using
new
isn't even an option as its not guaranteed 100% to allocate memory and there is no error handling and its prone to cause memory leaks if i wont make a proper cleanup.
So the code line above is responsible for storing the call function of a CompositeMenu and its name within an Action object, and the use of std::bind is needed because of the type of m_action the problem is that when i run the code it uses CompositeMenu's print instead of Action's print although i stored it as an Action object, is it undefined behavior because of the temporary object ? how should i fix this issue ?
the source.cpp i used for testing:
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 27 28 29 30 31 32 33
|
#include"Menu.h"
void a() { std::cout << 'a'; }
void b() { std::cout << 'b'; }
void c() { std::cout << 'c'; }
void d() { std::cout << 'd'; }
int main()
{
CompositeMenu menu;
CompositeMenu submenu;
submenu.addHeader("Sub menu");
menu.addHeader("Main menu");
menu.enableBoxedOptions();
Action optA("a", a);
Action optB("b", b);
Action optC("c", c);
Action optD("d", d);
menu.addChild(optA);
menu.addChild(optB);
menu.addChild(optC);
menu.addChild(optD);
menu.removeChild(optB);
/*
submenu.addChild(optA);
submenu.addChild(optB);
submenu.addChild(optC);
submenu.addChild(optD);
*/
menu.addChild(submenu);
menu.call();
return 0;
}
|
NOTE: Everything but the addChild method works perfectly and only adding a submenu causes the issue.