Question about list initialization

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:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
class Component // our base interface.
{
    public:
        virtual ~Component(){};
        virtual std::string Describe() const = 0;
};

class Object : public Component // Our core class to decorate.
{
    public:
        Object( const std::string& name ): mName( name ){}
        virtual std::string Describe() const{ return mName; }

    private:
        std::string mName;
};

class Decorator : public Component // Our base and derived decorators.
{
    public:
        Decorator( Component* comp ): mComponent( comp ){}
        virtual ~Decorator(){ delete mComponent; }

    protected:
        Component* mComponent;
};

class RocketBoosters : public Decorator
{
    public:
        RocketBoosters( Component* comp ): Decorator( comp ){}
        virtual std::string Describe() const
        {
            return mComponent->Describe() + " with rocket Boosters";
        }
};

class LaserCannon: public Decorator
{
    public:
        LaserCannon( Component* comp ): Decorator( comp ){}
        virtual std::string Describe() const
        {
            return mComponent->Describe() + " with laser Cannons";
        }
};


The parts I don't understand are these:
1
2
3
4
5
6
7
8
9
class RocketBoosters : public Decorator
{
    public:
        RocketBoosters( Component* comp ): Decorator( comp ){}
        virtual std::string Describe() const
        {
            return mComponent->Describe() + " with rocket Boosters";
        }
};


More specifically, here:
1
2
public:
        RocketBoosters( Component* comp ): Decorator( comp ){}


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
Last edited on
Take a look at this example code here:

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
#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.
Ooh ok, so RocketBooster's mComponent member is being set as the same as the Decorator's mComponent, right?
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.
Ooh ok perfect, I get it now... Thanks a lot for the explanation, very clear :)
Glad it helped :)
Topic archived. No new replies allowed.