开发者

Please explain this expression

class TestPtr : protected QSharedPointer<Test>

where Test is an abstract interface class. The TestPtr class should serve as the sma开发者_如何学Crt pointer class.

Does this mean class TestPtr is derived from the class Test ? Is class test enclosed in a smart pointer? I read that QSharedPointer is a template class. Could somebody please clarify?


What it doesn't mean

  1. That TestPtr derives from Test -- it does not.
  2. That class Test is enclosed in a smart pointer (but close: it means that instances of TestPtr will actually be smart pointers to Test without letting the world know about it except as the author of TestPtr explicitly chooses to)

What it means

It means that TestPtr wants to implement the functionality of a smart pointer to Test (which is what QSharedPointer<Test> is -- please note I have no idea exactly what QSharedPointer does, I 'm just "reading what's in the name").

To achieve this, the author of TestPtr plans to extensively use the functionality built into the inner works of QSharedPointer (that's why TestPtr inherits instead of having a QSharedPointer member -- to be able to use the protected members of QSharedPointer).

However, the author plans for TestPtr to not be equivalent to a QSharedPointer<TestPtr> even if the class has about the same functionality (we don't know the reason for this with the given information). That's why the inheritance is not public.


TestPtr is derived from the particular version of the template class QSharedPointer which has been instantiated with the class Test as its template parameter.


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.
}


QSharedPointer is a template class, and Test is what it templates.

TestPtr inherits from QSharedPointer (not Test - unless QSharedPointer inherits from Test - not enough info in the question to answer this, but its possible).

From the name it sounds like QSharedpointer is a smart pointer, but I am not familiar with this class to be able to answer.

In terms of protected inheritence, you might want to look at the following

http://www.parashift.com/c++-faq-lite/private-inheritance.html


No, class TestPtr is NOT derived from class Test. Class TestPtr does derive from

class QSharedPointer<Test>

which is really class QSharedPointer, expressed in terms of class Test.

Class QSharedPointer is a template class, which means a class that is written once but can then be used for Test, or other classes.

For example, similar to class QSharedPointer, you could have a class like this:

template <class T>
  class Vector
    {
    };

You could then have instances of this vector class which stores vectors of different things. For example Vector<int> is one class which stores ints, and Vector<bool> is a different class which stores booleans.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜