Does this mean class TestPtr is derived from the class Test ?
No. TestPtr
is derived from QSharedPointer
.
Is class test enclosed in a smart pointer?
Yes. Rather, QSharedPointer<Test>
is a smart pointer class that manages Test
pointers.
I read that QSharedpointer is a template class. Could somebody please clarify?
Templates abstract out types similar to how functions abstract values out of operations. For example,
int i,j;
...
i = 2*i+1;
j = 2*j+1;
can be abstracted to:
void foo(int &x) {
x = 2*x + i;
}
...
int i,j,
...
foo(i);
foo(j);
Similarly, rather than having separate classes such as:
class IntSharedPtr { typedef int value_type; ... };
class FloatSharedPtr { typedef float value_type; ...};
You can have a template class:
template <typename _T>
class SharedPtr { typedef _T value_type; ...};
typedef SharedPtr<int> IntSharedPtr;
typedef SharedPtr<float> FloatSharedPtr;
Finally, the protected
means that members of QSharedPointer
are only accessible in a TestPtr
by TestPtr
and its descendents. Suppose TestPtr
doesn't override operator *
and has a constructor that takes a Test
(or reference to a Test
). Then the following would fail, due to access restrictions:
int main() {
Test bar;
TestPtr pbar(bar);
*pbar; // Error: QSharedPointer<Test>::operator *() is protected within this context.
}