0

Recently I started working on a QT project, but there are more syntax rules than regular C++.

While declaring class and its constructor, in header file, we simply write

class MyObj : public QObject {
    Q_OBJECT
public:
    explicit MyObj(QObject* parent = nullptr, <params>, ...)
    .
    .
    <some more declarations>
private:
    .
    .
    <some more declarations>
};

In .cpp file, we can define this constructor as

MyObj::MyObj(QObject* parent, <params>) :
    QObject(parent),
    ...
    <some more parameter like arguments, that is what I am exactly asking>
{
    <regular initializations>
}

I am asking about the parameter like arguments after the : operator in the first line of declaring an initializing the class.

What are they, why QT needs these?

Note: In QT project examples, you can simply find the "Command Line Writer Async" example. In that project, a proper usage of this fact exists. I am using QT 5.15.

Voursstreds
  • 73
  • 1
  • 1
  • 6
  • 3
    That's a standard C++ feature called "member initializer lists". They provide a quick way of initializing your classes' base and member variables. See [What is this weird colon-member (" : ") syntax in the constructor?](https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) – Botje Aug 26 '21 at 11:39
  • Thanks for your suggestion; however, that looks different in their definition. Does this "member initialization list" the same thing? Is there any semantic changes for that operator in QT? – Voursstreds Aug 26 '21 at 11:47
  • QT cannot magically change the language. You are still writing C++. Can you explain how it looks different? – Botje Aug 26 '21 at 11:59
  • I don't know :). But it seemed differently. Does the member initialization list feature a necessity? Cannot we properly define the same think via methods and functions? @Botje – Voursstreds Aug 26 '21 at 12:02
  • 1
    @Voursstreds In certain situations you can pull off without member init list by assigning members in constructor body (like Java does). But you cannot do that if a) you need to initialize base class object, like in the example; b) you have `const` members; c) you have reference members; d) you have other non-assignable members (like `std::mutex`) – Yksisarvinen Aug 27 '21 at 08:02

1 Answers1

1

That's not an operator. That's a standard c++ feature called member initializer list.

For example

class Test
{
private:
    int test_val;
    some_type some_test;
public:
    Test() : 
    test_val(7),
    some_test()
    {}
    .
    .
    //whatever
}

In this example, when an instance of Test is constructed, the members are constructed(or initialized) as shown in the code. test_val is initialized with 7, and some_test is constructed via the call of the default constructor of some_type.

If you written the code above as follows.

class Test
{
private:
    int test_val;
    some_type some_test;
public:
    Test()
    {
        test_val=7;
        some_test=some_type(args...);
    }
}

This is equivalent to.

class Test
{
private:
    int test_val;
    some_type some_test;
public:
    Test() : 
    test_val(),
    some_test()
    {
        test_val=7;
        some_test=some_type(args...);
    }
}

Which means that every member is constructed(or initialized) by-default, and then is copy-constructed as requested in the body of Test::Test(). The mere purpose for member initialization list is to specify how the members should be initalized

Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

Karen Baghdasaryan
  • 2,407
  • 6
  • 24