[Qt-interest] Const correctness with QSharedPointer
Colin Kern
colin.kern at gmail.com
Mon Dec 7 23:26:56 CET 2009
On Mon, Dec 7, 2009 at 4:31 PM, Stephen Jackson <spjackson42 at gmail.com> wrote:
> 2009/12/7 Brad Howes :
>> On Dec 7, 2009, at 3:31 PM, Colin Kern wrote:
>>
>>> I want to pass a QSharedPointer to a function as a const pointer, so
>>> that the compiler will enforce that I don't mutate the object the
>>> shared pointer is pointing to. Can I do this with "void func(const
>>> QSharedPointer<A> p);" or does that just enforce that I won't change
>>> what the shared pointer is pointing to?
I'm going to hijack my own thread here, since I have another question
about QSharedPointers. Is there a way to maintain polymorphism when
using QSharedPointers? This code works:
class A { };
class B : public A { };
void func(A* p) { };
int main()
{
B *p = new B();
func(p);
}
but this doesn't compile:
#include <QSharedPointer>
class A { };
class B : public A { };
void func(QSharedPointer<A> p) { };
int main()
{
QSharedPointer<B> p(new B());
func(p);
}
Thanks,
Colin
More information about the Qt-interest-old
mailing list