-2
template< class arg >
class Master;
class StageA;
class StageB;

class Slave
    :public Master<Slave>
{
public:
    Slave();

    StageA  start{this};
    StageB  stop{this};
 };

Slave::Slave()
    :Master<Slave>( start ){}

Newbie to the concept of templates. I am unable to understand the usage of the last line in this piece. Can some one please explain what is happening here?

HardCoder
  • 249
  • 3
  • 12

2 Answers2

2

Besides the fact that, like already stated by others in the comments, you cannot forward declare a template class like this, and that this code should probably not be in the same file (or the constructors implementation should be inlined), the answer to your original question is:

Slave is derived from Master. Master is a template class.
So, what happens in the line in question is that the constructor of the Master class is called with start as parameter.
This here is an example of the curiously recurring template pattern, where a class (Slave) is derived from another class (Master) and passes itself as template parameter.

Rene
  • 2,466
  • 1
  • 12
  • 18
  • Thankyou. I just read about CRTP after your comment. https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern – HardCoder Aug 20 '18 at 06:43
1

I am unable to understand the usage of the last line in this piece. Can some one please explain what is happening here?

This usage is not specific to templates. This is called member initialization list used with constructors of normal classes as well. You can read about in detail here.

But, as others have pointed out, at least one major issue with your posted code.

P.W
  • 26,289
  • 6
  • 39
  • 76