Related Functions

QxtPimpl Class Reference

Hide private details of a class. More...

List of all members.

Related Functions

(Note that these are not member functions.)


 QXT_DECLARE_PRIVATE(PUB)
 QXT_DECLARE_PUBLIC(PUB)
 QXT_INIT_PRIVATE(PUB)
 QXT_D(PUB)
 QXT_P(PUB)

Detailed Description

Hide private details of a class.

Application code generally doesn't have to be concerned about hiding its implementation details, but when writing library code it is important to maintain a constant interface, both source and binary. Maintaining a constant source interface is easy enough, but keeping the binary interface constant means moving implementation details into a private class. The PIMPL, or d-pointer, idiom is a common method of implementing this separation. QxtPimpl offers a convenient way to connect the public and private sides of your class.

Getting Started

Before you declare the public class, you need to make a forward declaration of the private class. The private class must have the same name as the public class, followed by the word Private. For example, a class named MyTest would declare the private class with:

class MyTestPrivate;

The Public Class

Generally, you shouldn't keep any data members in the public class without a good reason. Functions that are part of the public interface should be declared in the public class, and functions that need to be available to subclasses (for calling or overriding) should be in the protected section of the public class. To connect the private class to the public class, include the QXT_DECLARE_PRIVATE macro in the private section of the public class. In the example above, the private class is connected as follows:

private:
    QXT_DECLARE_PRIVATE(MyTest);

Additionally, you must include the QXT_INIT_PRIVATE macro in the public class's constructor. Continuing with the MyTest example, your constructor might look like this:

MyTest::MyTest() {
    // initialization
    QXT_INIT_PRIVATE(MyTest);
}

The Private Class

As mentioned above, data members should usually be kept in the private class. This allows the memory layout of the private class to change without breaking binary compatibility for the public class. Functions that exist only as implementation details, or functions that need access to private data members, should be implemented here.

To define the private class, inherit from the template QxtPrivate class, and include the QXT_DECLARE_PUBLIC macro in its public section. The template parameter should be the name of the public class. For example:

class MyTestPrivate : public QxtPrivate<MyTest> {
public:
    MyTestPrivate();
    QXT_DECLARE_PUBLIC(MyTest);
};

Accessing Private Members

Use the qxt_d() function (actually a function-like object) from functions in the public class to access the private class. Similarly, functions in the private class can invoke functions in the public class by using the qxt_p() function (this one's actually a function).

For example, assume that MyTest has methods named getFoobar and doBaz(), and MyTestPrivate has a member named foobar and a method named doQuux(). The code might resemble this example:

int MyTest::getFoobar() {
    return qxt_d().foobar;
}

void MyTestPrivate::doQuux() {
    qxt_p().doBaz(foobar);
}

Friends And Related Function Documentation

QXT_D (   PUB ) [related]

Returns a reference in the current scope named "d" to the private class.

This function is only available in a class using QXT_DECLARE_PRIVATE.

Definition at line 148 of file qxtpimpl.h.

QXT_DECLARE_PRIVATE (   PUB ) [related]

Declares that a public class has a related private class.

This shuold be put in the private section of the public class. The parameter is the name of the public class.

Definition at line 129 of file qxtpimpl.h.

QXT_DECLARE_PUBLIC (   PUB ) [related]

Declares that a private class has a related public class.

This may be put anywhere in the declaration of the private class. The parameter is the name of the public class.

Definition at line 135 of file qxtpimpl.h.

QXT_INIT_PRIVATE (   PUB ) [related]

Initializes resources owned by the private class.

This should be called from the public class's constructor, before qxt_d() is used for the first time. The parameter is the name of the public class.

Definition at line 142 of file qxtpimpl.h.

QXT_P (   PUB ) [related]

Creates a reference in the current scope named "q" to the public class.

This macro only works in a class using QXT_DECLARE_PUBLIC.

Definition at line 154 of file qxtpimpl.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines