operator= - code in overloaded function doesn't get called

Discussion in 'C++' started by Tobias Langner, Jul 31, 2003.

  1. I overloaded the operator= - but in the program, if the line p3=p1 gets
    executed, the program doesn't jump to the method.

    The class definition and the test code.

    template <class T, template <class> class OwnershipPolicy>
    class SmartPtr : public OwnershipPolicy<T> {
    public:
    explicit SmartPtr(T* pointer);
    SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
    SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T, OwnershipPolicy>
    &);
    ~SmartPtr();
    T& operator*() const;
    T* operator->() const;

    private:
    void deleteObject();
    void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
    SmartPtr();
    T* pointer_;

    };

    //The Testcode:

    bool testSmartPtr() {
    SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
    RefCount>(new LongWrapper(1));
    SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
    RefCount>(new LongWrapper(20));
    SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
    RefCount>(*p2);



    std::cout<<"p1=p3\n";
    p3=p1;

    std::cout<<"delete p1\n";
    delete p1;
    std::cout<<"delete p2\n";
    delete p2;
    std::cout<<"delete p3\n";
    delete p3;

    return true;
    }
    Tobias Langner, Jul 31, 2003
    #1
    1. Advertising

  2. "Tobias Langner" <> wrote in message
    news:bgal7l$16c$07$-online.com...
    > I overloaded the operator= - but in the program, if the line p3=p1 gets
    > executed, the program doesn't jump to the method.
    >
    > The class definition and the test code.
    >
    > template <class T, template <class> class OwnershipPolicy>
    > class SmartPtr : public OwnershipPolicy<T> {
    > public:
    > explicit SmartPtr(T* pointer);
    > SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
    > SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T,

    OwnershipPolicy>
    > &);
    > ~SmartPtr();
    > T& operator*() const;
    > T* operator->() const;
    >
    > private:
    > void deleteObject();
    > void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
    > SmartPtr();
    > T* pointer_;
    >
    > };
    >
    > //The Testcode:
    >
    > bool testSmartPtr() {
    > SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
    > RefCount>(new LongWrapper(1));
    > SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
    > RefCount>(new LongWrapper(20));
    > SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
    > RefCount>(*p2);
    >
    >
    >
    > std::cout<<"p1=p3\n";
    > p3=p1;
    >
    > std::cout<<"delete p1\n";
    > delete p1;
    > std::cout<<"delete p2\n";
    > delete p2;
    > std::cout<<"delete p3\n";
    > delete p3;
    >
    > return true;
    > }


    Well that's because p1, p2 and p3 are pointers.

    I think perhaps what you meant to write is

    SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
    LongWrapper(1));
    SmartPtr<LongWrapper, RefCount> p2=SmartPtr<LongWrapper, RefCount>(new
    LongWrapper(20));
    SmartPtr<LongWrapper, RefCount> p3=p2;
    std::cout<<"p1=p3\n";
    p3=p1;

    You seem to be getting your smart pointers and your ordinary pointers mixed
    up.

    john
    John Harrison, Jul 31, 2003
    #2
    1. Advertising

  3. "Tobias Langner" <> wrote in message
    news:bgal7l$16c$07$-online.com...
    > I overloaded the operator= - but in the program, if the line p3=p1 gets
    > executed, the program doesn't jump to the method.
    >
    > The class definition and the test code.
    >
    > template <class T, template <class> class OwnershipPolicy>
    > class SmartPtr : public OwnershipPolicy<T> {
    > public:
    > explicit SmartPtr(T* pointer);
    > SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
    > SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T,

    OwnershipPolicy>
    > &);
    > ~SmartPtr();
    > T& operator*() const;
    > T* operator->() const;
    >
    > private:
    > void deleteObject();
    > void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
    > SmartPtr();
    > T* pointer_;
    >
    > };
    >
    > //The Testcode:
    >
    > bool testSmartPtr() {
    > SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
    > RefCount>(new LongWrapper(1));
    > SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
    > RefCount>(new LongWrapper(20));
    > SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
    > RefCount>(*p2);
    >
    >
    >
    > std::cout<<"p1=p3\n";
    > p3=p1;


    'p3' and 'p1' are pointers, not objects. That is why the operator= for
    the 'object' does not get called, but the operator= for the 'pointer'. The
    original value for 'p3' is lost, meaning you also have a memory leak.

    > std::cout<<"delete p1\n";
    > delete p1;
    > std::cout<<"delete p2\n";
    > delete p2;
    > std::cout<<"delete p3\n";
    > delete p3;


    You have undefined behaviour here, because above you made 'p1' and 'p3'
    point to the same object, which you now try to delete a second time. Your
    program might have crashed here.

    > return true;
    > }


    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Jul 31, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Bhardwaj
    Replies:
    8
    Views:
    849
    Andrey Tarasevich
    Jul 14, 2003
  2. John Goche
    Replies:
    2
    Views:
    337
    Frederick Gotham
    Sep 4, 2006
  3. Afshin
    Replies:
    0
    Views:
    286
    Afshin
    Aug 12, 2011
  4. Afshin
    Replies:
    0
    Views:
    287
    Afshin
    Aug 12, 2011
  5. Afshin
    Replies:
    7
    Views:
    417
    Victor Bazarov
    Aug 16, 2011
Loading...

Share This Page