I have a doubt about a line of the code written by my professor.
The relevant function is:
std::vector<AbstractButton*> removeUnchecked() {
std::vector<AbstractButton*> v;
CheckBox* p;
for(auto it = Buttons.begin(); it != Buttons.end(); ++it) {
p = const_cast<CheckBox*>(dynamic_cast<const CheckBox*>(*it));
if(p && !(p->isChecked())) {
v.push_back(p);
it = Buttons.erase(it); --it;
}
}
return v;
}
The class Gui
has a std::list<const AbstractButton*> Buttons
and the function std::vector<AbstractButton*> removeUnchecked(){}
wants a vector
as its return type. We had to remove from the Gui
every checkable buttons with the attribute checked == false
and put it into the returned vector.
The professor wrote:
CheckBox* p = const_cast<CheckBox*>(dynamic_cast<const CheckBox*>(*it));
performing a dynamic_cast
first, and then a const_cast
.
If I had written:
CheckBox* p = dynamic_cast<CheckBox*>(const_cast<AbstractButton*>(*it))
const_cast
first, and then dynamic_cast
, would it be the same thing?