Hi, I'm learning about design patterns, specifically about the decorator pattern. I don't really understand a part of the example code where they use list initialization. Here are the classes that the example uses:
What is going on there, why is it being initialized like that?
Sorry if the question is a bit unclear and convoluted..
Thanks a lot in advance for taking the time
#include <iostream>
class A
{
public:
A(int l): a(l){}
protected:
int a;
};
class B : public A
{
public:
B(int g) : A(g) {}
void show()
{
std::cout << this->a;
}
};
int main()
{
B object(12);
object.show();
}
Output:
12
The constructor for B calls the constructor for A in order to set the value of "int a" in an expedient way.
The same is happening in your example. RocketBoosters constructor takes in a Component pointer, then it calls the Decorator constructor in order to let it set the variable "mComponent" for it.
Well, there's only ONE mComponent variable. RocketBoosters has access to the members of Decorator. By calling the Decorator constructor, you're actually setting the variable. You can think of inheritance as a parent. You created Rocketboosters inheriting from Decorator, so Rocketboosters simply used the inherited constructor in order to set the mComponent variable.