1

What does the following C++ syntax mean?

my_s_module::my_s_module(mylib::cont const& c)
    :mylib::s_module{c}
{
    // Some content;
}

It looks like inheritance to me. But I know that for inheritance the syntax is a follows:

class Child: public Parent
{
}

And in the first example we have something like that:

Child(some_type const& x): public Parent{x}
{
}

So, I do not know what it means. Could anybody please explain me this syntax?

ADDED

I probably need to add, that in the comments to this code it is written that it is "Constructor of the module". But what does it mean? I know what a constructor of a class mean but what is a constructor of a module?

Roman
  • 124,451
  • 167
  • 349
  • 456

3 Answers3

0

This is a constructor of class my_s_module defined outside the class definition. This constructor initializes a member s_module of a nested class mylib to c, which is the argument of the constructor.

cpp
  • 3,743
  • 3
  • 24
  • 38
0

From your description, it's just an initialisation list in a constructor.

After the colon, you specify the class members and initial values for them.

For example: http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/

Karadur
  • 1,226
  • 9
  • 16
0

It's a regular initialization list in the constructor of the class my_s_module, which inherits from s_module, which is in the namespace (or class) mylib.

The code should look something like this:

namespace mylib
{ 
    class cont
    {
       // ...
    };

    class s_module
    {
    public:
        s_module(cont const&);
        // ...
    };
}

class my_s_module : public mylib::s_module
{
public:
    my_s_module(mylib::cont const &);
};

// Constructor implementation
my_s_module::my_s_module(mylib::cont const& c)
    : mylib::s_module{c}  // Initialize the base class. The {} around the argument is a C++11 feature.
{
    // Some content;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82