Behaviour of const KSharedPtrs

Stefan Teleman steleman at nyc.rr.com
Sat Nov 13 00:54:46 GMT 2004


Should the following code compile ?

#include <iostream>
using namespace std;

template<class T>
class Foo
{
    public:
            Foo() : _t(0) { }
            Foo (const Foo& rhs) : _t(rhs._t) { }
            ~Foo() { }
            Foo& operator= (const Foo& rhs)
            {
                if (this != &rhs)
                    _t = rhs._t;

                return *this;
            }
            void set (T* t) { _t = t; }
            T* get() const { return _t; }

    private:
            T* _t;

};

int
main (int argc, char* argv[])
{
    Foo<int> foo;
    int x = 7;
    int y = 9;
    foo.set(&x);
    std::cerr << "foo = " << *(foo.get()) << endl;
    foo.get() = &y;
    std::cerr << "foo = " << *(foo.get()) << endl;
    return 0;
}

--Stefan

-------------------------

On Friday 12 November 2004 19:48, Richard Smith wrote:
> Not for a smart pointer, it's not. The thing const-correctness
> covers for a pointer (or by extension a smart pointer) is not
> allowing changes to the value of the pointer (ie, disallowing
> assignment). You're getting const-correctness of the pointer
> confused with const-correctness of the pointee.

-- 
Stefan Teleman          'Nobody Expects the Spanish Inquisition'
steleman at nyc.rr.com                          -Monty Python





More information about the kde-core-devel mailing list