0

I am trying to understand what the following code is doing and why:

class Beverage {
    std::string m_description;
protected:
    Beverage(std::string aDescription):m_description(aDescription){}

The part I am trying to understand is the ":m_description(aDescription){}" I don't understand what that is declaring.

Code from Olivianeacsu

Athomas1
  • 39
  • 5
  • @NathanOliver, shall we reopen on the grounds that a good answer should mention the protected constructor? – Bathsheba Apr 19 '16 at 13:15
  • 3
    @Bathsheba I think that is tangential. the OP specifically states they do not understand the `:m_description(aDescription){}` part. – NathanOliver Apr 19 '16 at 13:16

1 Answers1

1

:m_description(aDescription) is initialising the member m_description.

This is preferred to writing m_description = aDescription in the constructor body, as for one thing, m_description can then be const and that member type does not require a default constructor.

The fact that the constructor is protected means that it can only be called from base classes. The compiler will also not generate a default constructor in this instance, so this means that Beverage must be inherited.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483