[Qt-interest] Const correctness with QSharedPointer

Brad Howes howes at ll.mit.edu
Tue Dec 8 22:36:56 CET 2009


On Dec 7, 2009, at 4:31 PM, Stephen Jackson wrote:

> I'm sorry but both of these options permit mutation of the object
> pointed at, as demonstrated by the following code.
> 
> The commented out options 1 & 2 both compile, permit the increment and print 5.
> 
> The uncommented version is what you need to throw a compile-time error on ++*p;
> 
> #include <QtGui>
> #include <iostream>
> 
> // 1. void foo(const QSharedPointer<int> p)
> // 2. void foo(const QSharedPointer<int> & p)
> void foo(const QSharedPointer<const int> p)


Doh! Thanks. In haste I forgot the second const. I will note that you don't need the first const if missing an '&' since you are working with a local variable anyway.

void foo(const QSharedPointer<const int>&p)

or

void foo(QSharedPointer<const int> p)

-- 
Brad Howes
Group 42
MIT Lincoln Laboratory • 244 Wood St. • Lexington, MA 02173
Phone: 781.981.5292 • Fax: 781.981.3495 • Secretary: 781.981.7420








More information about the Qt-interest-old mailing list