-2

I downloaded an example from the blackberry website. I noticed they have some values before the curly braces, and after a colon, seperated by commas.

What are these for, and how do they work?

Edit: Is this just another way of instantiating values? The same as setting these values inside of the curly braces?

using namespace bb::cascades;
using namespace bb::pim::contacts;

//! [0]
AddressBook::AddressBook(QObject *parent)
    : QObject(parent)
    , m_contactService(new ContactService(this))
    , m_model(new GroupDataModel(this))
    , m_contactViewer(new ContactViewer(m_contactService, this))
    , m_contactEditor(new ContactEditor(m_contactService, this))
    , m_currentContactId(-1)
{
    // Disable grouping in data model
    m_model->setGrouping(ItemGrouping::None);

    // Ensure to invoke the filterContacts() method whenever a contact has been added, changed or removed
    bool ok = connect(m_contactService, SIGNAL(contactsAdded(QList<int>)), SLOT(filterContacts()));
    Q_ASSERT(ok);
    ok = connect(m_contactService, SIGNAL(contactsChanged(QList<int>)), SLOT(filterContacts()));
    Q_ASSERT(ok);
    ok = connect(m_contactService, SIGNAL(contactsDeleted(QList<int>)), SLOT(filterContacts()));
    Q_ASSERT(ok);

    // Fill the data model with contacts initially
    filterContacts();
}
Bobby W
  • 836
  • 8
  • 22
  • 2
    [Constructor's initializer list](http://en.cppreference.com/w/cpp/language/initializer_list). – 101010 Nov 04 '15 at 20:14
  • 2
    That's not a semi-colon, that's a colon. – Galik Nov 04 '15 at 20:17
  • 1
    The top four google hits for "constructor colon" point to answers for this question on this very site. DuckDuckGo and Bing would have worked too if you do not like Google, as would "[c++] constructor colon" in the SO search. Downvoted for lack of research effort. – Baum mit Augen Nov 04 '15 at 20:22
  • Man, I wish they taught member initializer lists in schools. – user4581301 Nov 04 '15 at 22:14

2 Answers2

5

That's a member initializer list (and it's a colon, not a semicolon).

It initializes the member with the value in the parentheses, so (for example) m_contactService(new ContactService(this)) is approximately the same as if you put m_contactService = new ContactService(this); inside the body of the constructor.

There are a few differences though--the member initializer list does actually initialize instead of assigning a value. That means it can be used for things like const values, the base class, and references, to which assignment isn't allowed.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

These are member initializer lists.

They are used to initialize members of an object. Note the difference between initialization and assignment. You could also "initialize" the members in the constructor but that would actually be an assignment. That means, the member would've been initialized (default constructed) beforehand.

Using a member initializer list is more efficient than assignment within the constructor and is good C++ style.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67