views:

60

answers:

4

Say i create a derived class as below,

    class CHIProjectData : public QObject 
{ 
CHIProjectData(QMap<QString,QString> aProjectData,
                           CHIMetaData* apMetaData = 0,
                           QObject* parent = 0); 
    private:
            QMap<QString,QString> m_strProjectData;
            CHIAkmMetaData* m_pMetaData; 
};

and i implement like,

CHIProjectData::CHIProjectData(QMap<QString,QString> aProjectData,
                               CHIMetaData* apMetaData,
                               QObject* aParent)
    :m_strProjectData(aProjectData),
    m_pMetaData(apMetaData),
    QObject(aParent)
{

}

i know i initiate the member variables m_strProjectData, m_pMetaData in the constructor. but what does the last part "QObject(aParent)" do? does it create an object of base class and consider that as a member variable?

A: 

A class instance that is a base of a derived class is sometimes called a "base class subobject", so in some sense a base class is a distinct 'part' of your derived class.

In your constructor's initializer list, the QObject(aParent) is choosing how the base class is constructed. In this case a single parameter constructor is being used. If the base class were omitted from the initializer list of your derived class' constructor its default constructor would be used.

It's not strictly a member variable, although like a member variable it's a constituent part of your derived class along with any other base class subobjects and other members.

Charles Bailey
+1  A: 

Essentially, that is what is happening under the hood. The base class parts of your object, like its data members, are called subobjects.

The notion of initializing a base as in QObject(aParent) is similar to initializing a member, but bases are always initialized first. Therefore, it would be clearer to list QObject before the members, so the list of initializers is in chronological order.

The order of initialization always follows the order the bases are named after class and the order the members are declared, no matter how the initializer sequence is written.

Potatoswatter
A: 

Not quite. It tells the base class's constructor what to do. Imagine this class:

class A
{
public:
  A(int val)
    : value(val)
  {
  }
protected:
  int value;
};

To construct A, you have to pass an int. This is always true, even if you derive from it.

Say you are class B, which derives from A. You are an A, but you still have to tell the A part of your class how to construct itself:

class B : public A
{
public:
  B()
    : A(5)
  {
  }

  int GetValue()
  {
    return value;
  }
};

The members of A become your members, though, because you are an A.

In machine memory, the scenario is kind of how you described, but only in simple cases. It becomes more complicated with virtual functions, multiple inheritance, and virtual inheritance. If you stick to the is-a and has-a relationships, then you may avoid some headaches :)

Merlyn Morgan-Graham
+4  A: 

QObject(aParent) calls QObject's constructor with the aParent parameter. QObject is not a member variable in this case. It may seem like a subtle point, but its an important one because the way you access the properties and methods of a subobject requires different syntax than as for a member variable.

Here's an analogy to try to understand the difference between a subobject and a member variable.

In the movie "Batman: The Dark Night" there is a scene where Batman is pursuing the bad guy in his car. But the car becomes damaged and unusable, and he has to escape. At that point Batman pushes a button and part of the car detatches from the rest, becoming a motorcycle. This is kind of like a subobject. The car is a motorcycle.

Now consider the case of an RV towing a smaller vehicle, the likes of which are frequently seen on the highways of America. In this case, the RV has a vehicle. The vehicle is a member variable of the RV.

John Dibling
Up-vote for making is-a analogous to a Batman movie.
MadcapLaugher
That's not how I understand is-a though. batman's car is both *instance-of* motorcycle and car (not is-a, it's an instance!). While a lion is-a animal (both classes).
Frank
@Frank: Admittedly, its not a perfect analogy. But I thought it got the idea across OK, and it's the best analogy I could think of!
John Dibling