just have those questions about those code sinppets from the C++ GUI Programming with Qt 4 book:

GoToCellDialog::GoToCellDialog(QWidget *parent):QDialog(parent)

Does that mean we are inheriting QDialog(parent)? Or, what exactly does this mean?

setupUi(this);

Here, this code snippet is part of the gotocelldialog.cpp file, which is the implementation of gotocelldialog.h header file. What do we mean by this in this context? What are we trying to setup? And, what kind of setup will that be?

Thanks.

Recommended Answers

All 3 Replies

No, GoToCellDialog::GoToCellDialog(QWidget *parent):QDialog(parent) is a constructor. The : says that the stuff that follows is the initialisation list. It's a way of Initialising the members of a class when the constructor is called. To simplify:

class A{

    public:
        A( int iNum );   // Constructor that takes an argument
        void someFunction();

    private:
        int m_iNumber;
        bool m_bFlag;
};

// Constructor with initialisation list
A::A( int iNum ) : m_iNumber( iNum ), m_bFlag(true)
{
}

The two member variables are set to the value of iNum for m_iNumber and true for the bool variable. Does that help at all?

I have that book too, it's a good one :o) You might also consider getting "Effective C++: 50 Specific Ways to Improve Your Programs and Designs" by Scott Meyers. He makes a good case for always initialising member variables this way if you can

No, GoToCellDialog::GoToCellDialog(QWidget *parent):QDialog(parent) is a constructor. The : says that the stuff that follows is the initialisation list. It's a way of Initialising the members of a class when the constructor is called. To simplify:

class A{

    public:
        A( int iNum );   // Constructor that takes an argument
        void someFunction();

    private:
        int m_iNumber;
        bool m_bFlag;
};

// Constructor with initialisation list
A::A( int iNum ) : m_iNumber( iNum ), m_bFlag(true)
{
}

The two member variables are set to the value of iNum for m_iNumber and true for the bool variable. Does that help at all?

I have that book too, it's a good one :o) You might also consider getting "Effective C++: 50 Specific Ways to Improve Your Programs and Designs" by Scott Meyers. He makes a good case for always initialising member variables this way if you can

Thanks for your reply.

So, is:

A::A( int iNum ) : m_iNumber( iNum ), m_bFlag(true)
{
}

the same as:

A::A( int iNum )
{
m_iNumber = iNum;
m_bFlag = true;
}

?

And, how does this work for the case I asked about? In other words, how can we write it in the latter form?

Thanks.

A::A( int iNum ) : m_iNumber( iNum ), m_bFlag(true)
{
}

the same as:

A::A( int iNum )
{
m_iNumber = iNum;
m_bFlag = true;
}

Yes and No. For the simple case, the two are functionally equivalent. However, there are special cases where you can only use the initializer list to assign variables: base classes and const members.
In the base class scenario you do not actually have a handle to the class in question (there is no explicit this->parent or parent to assign to). This is not a problem if your base class constructor does not take arguments but if you need to pass arguments to the constructor the only place to do so is the initializer list.
In the const member scenario you would get an error about trying to assign to a const if you tried something like const_var = some_value; in the constructor body. You are, however, able to do klass() : const_var(some_value) {} .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.