Smart pointer implementation.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maadhuu

    #1

    Smart pointer implementation.

    Hello,
    I have tried this smart pointer implementation, but it is not working and
    I am not able to figure out why .......Also, can you please suggest more
    effective way/s of doing the same ???
    Thank you,
    Maadhuu.

    //Smart.h

    #ifndef _SMART_H
    #define _SMART_H
    #include<iostre am>

    using namespace std;

    template<typena me T>
    struct PointerToT
    {
    explicit PointerToT<T>(T * realPtr=0):poin tee(realPtr) {cout << "in
    constructor";}
    PointerToT(Poin terToT& rhs)
    {
    pointee = rhs.pointee;
    rhs.pointee = 0;
    }
    PointerToT<T>& operator=(Point erToT<T>& that)
    {
    if(this == &that) return *this;
    delete pointee ;
    pointee = that.pointee;
    delete that.pointee ;
    return *this;

    }
    ~PointerToT<T>( ) { delete pointee;}

    T& operator*() const { return *pointee;}
    T* operator->() const { return &**this;} // &(this->operator*()) -
    &(*this).operat or*() -
    //& (* *this);

    private:
    T* pointee;
    };

    #endif //_SMART_H

    //smart.cpp

    #include<iostre am>
    #include "smart.h"
    using namespace std;

    void printNode(ostre am &os,const PointerToT<char >& ptr)
    {
    os << *ptr;
    }
    int main()
    {
    char *p = "abcdef";
    cout << p;
    PointerToT<char > ptr(p); //not working.

    printNode(cout, ptr);

    PointerToT<char > ptr1(ptr);
    PointerToT<char > ptr2;
    ptr2 = ptr1;

    return 0;
    }

    Thank You once again.

  • Matthias Kaeppler

    #2
    Re: Smart pointer implementation.

    maadhuu wrote:[color=blue]
    > I have tried this smart pointer implementation, but it is not working and
    > I am not able to figure out why .......Also, can you please suggest more
    > effective way/s of doing the same ???[/color]

    Sure: #include <boost/shared_ptr.hpp>

    Time saving and effective! ;-)

    Regards,
    Matthias

    Comment

    • Neelesh Bodas

      #3
      Re: Smart pointer implementation.


      maadhuu wrote:[color=blue]
      > Hello,
      > I have tried this smart pointer implementation, but it is not working and
      > I am not able to figure out why .......Also, can you please suggest more
      > effective way/s of doing the same ???[/color]

      Donot reinvent the wheel. Look up auto_ptr. (They are not the "all
      purpose" smart pointers, but they will suffice your needs as indicated
      from this program)

      Anyways, your program is working the way it is expected to work ;-)

      Comment

      • Jonathan Mcdougall

        #4
        Re: Smart pointer implementation.

        maadhuu wrote:[color=blue]
        > Hello,
        > I have tried this smart pointer implementation, but it is not working and
        > I am not able to figure out why .......Also, can you please suggest more
        > effective way/s of doing the same ???
        > Thank you,
        > Maadhuu.
        >
        > //Smart.h
        >
        > #ifndef _SMART_H
        > #define _SMART_H
        > #include<iostre am>
        >
        > using namespace std;[/color]

        Don't do that in a header.


        [color=blue]
        > template<typena me T>
        > struct PointerToT
        > {
        > explicit PointerToT<T>(T * realPtr=0):poin tee(realPtr) {cout << "in
        > constructor";}[/color]

        Try to be careful with line breaks when posting.
        [color=blue]
        > PointerToT(Poin terToT& rhs)
        > {
        > pointee = rhs.pointee;[/color]

        Why not initialization?
        [color=blue]
        > rhs.pointee = 0;
        > }
        > PointerToT<T>& operator=(Point erToT<T>& that)
        > {
        > if(this == &that) return *this;
        > delete pointee ;
        > pointee = that.pointee;
        > delete that.pointee ;[/color]

        Don't these two last lines ring a bell?
        [color=blue]
        > return *this;
        >
        > }
        > ~PointerToT<T>( ) { delete pointee;}
        >
        > T& operator*() const { return *pointee;}
        > T* operator->() const { return &**this;} // &(this->operator*()) -
        > &(*this).operat or*() -
        > //& (* *this);[/color]

        What's all that? Check what you post!
        [color=blue]
        > private:
        > T* pointee;
        > };
        >
        > #endif //_SMART_H
        >
        > //smart.cpp
        >
        > #include<iostre am>
        > #include "smart.h"
        > using namespace std;
        >
        > void printNode(ostre am &os,const PointerToT<char >& ptr)
        > {
        > os << *ptr;
        > }
        > int main()
        > {
        > char *p = "abcdef";
        > cout << p;
        > PointerToT<char > ptr(p); //not working.[/color]

        This should work ok.
        [color=blue]
        > printNode(cout, ptr);
        >
        > PointerToT<char > ptr1(ptr);
        > PointerToT<char > ptr2;
        > ptr2 = ptr1;
        >
        > return 0;[/color]

        Now, assuming you corrected the operator= above, this will try to
        delete something that was not allocated with new. This whole code is
        equivalent to

        delete "abcdef";

        which is clearly not legal. Smart pointers are meant to be used with
        dynamically allocated memory!
        [color=blue]
        > }
        >
        > Thank You once again.[/color]

        And what about std::auto_ptr?


        Jonathan

        Comment

        Working...