Smart Pointer help

Discussion in 'C++' started by Protoman, Jul 4, 2006.

  1. Protoman

    Protoman Guest

    Here's a non intrusive reference counting smart pointer class I'm
    working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
    declaration of `SmrtPtrDB' with no type" error.

    Code:

    SmrtPtr.hpp

    #pragma once

    template<class T>
    class SmrtPtr
    {
    public:
    explicit SmrtPtr(T* obj):ptr(obj)
    {
    DataBase.add();
    for(;;)
    {
    if(isInvalid())
    delete this;
    }
    }
    SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.obj){DataBase.add()}
    ~SmrtPtr(){delete ptr; DataBase.sub()}
    T& operator*(){return *ptr;}
    T* operator->(){return ptr;}
    T** operator&(){return &ptr;}
    private:
    static SmrtPtrDB<T> DataBase;
    bool isInvalid()
    {
    for(;;)
    if(!DataBase.status())
    return true;
    else return false;
    }
    T* ptr;
    };


    SmrtPtrDB.hpp

    #pragma once
    #include "SmrtPtr.hpp"

    template<class T>
    class SmrtPtrDB
    {
    public:
    SmrtPtrDB():num(0){}
    ~SmrtPtrDB(){}
    void add(){num++;}
    void sub(){num--);
    int status(){return num;}
    private:
    int num;
    };

    Could you help me out on this? I'm not even sure if I'm coding the
    non-intrusive reference counting correctly. Thanks!!!!!
    Protoman, Jul 4, 2006
    #1
    1. Advertising

  2. Protoman

    Marco Wahl Guest

    Protoman wrote:
    > Here's a non intrusive reference counting smart pointer class I'm
    > working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
    > declaration of `SmrtPtrDB' with no type" error.
    >
    > Code:
    >
    > SmrtPtr.hpp
    >
    > #pragma once
    >
    > template<class T>
    > class SmrtPtr
    > {
    > public:
    > explicit SmrtPtr(T* obj):ptr(obj)
    > {
    > DataBase.add();
    > for(;;)
    > {
    > if(isInvalid())
    > delete this;
    > }
    > }
    > SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.obj){DataBase.add()}
    > ~SmrtPtr(){delete ptr; DataBase.sub()}
    > T& operator*(){return *ptr;}
    > T* operator->(){return ptr;}
    > T** operator&(){return &ptr;}
    > private:
    > static SmrtPtrDB<T> DataBase;
    > bool isInvalid()
    > {
    > for(;;)
    > if(!DataBase.status())
    > return true;
    > else return false;
    > }
    > T* ptr;
    > };
    >
    >
    > SmrtPtrDB.hpp
    >
    > #pragma once
    > #include "SmrtPtr.hpp"
    >
    > template<class T>
    > class SmrtPtrDB
    > {
    > public:
    > SmrtPtrDB():num(0){}
    > ~SmrtPtrDB(){}
    > void add(){num++;}
    > void sub(){num--);
    > int status(){return num;}
    > private:
    > int num;
    > };
    >
    > Could you help me out on this? I'm not even sure if I'm coding the
    > non-intrusive reference counting correctly. Thanks!!!!!


    You use the name 'SmrtPtrDB' in file 'SmrtPtr.hpp' before any
    declaration.

    HTH
    Marco Wahl, Jul 4, 2006
    #2
    1. Advertising

  3. Protoman

    TB Guest

    Protoman skrev:
    > Here's a non intrusive reference counting smart pointer class I'm
    > working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
    > declaration of `SmrtPtrDB' with no type" error.
    >
    > Code:
    >

    <snip>
    >
    > SmrtPtrDB.hpp
    >
    > #pragma once
    > #include "SmrtPtr.hpp"
    >
    > template<class T>
    > class SmrtPtrDB
    > {
    > public:
    > SmrtPtrDB():num(0){}
    > ~SmrtPtrDB(){}
    > void add(){num++;}
    > void sub(){num--);


    void sub() { num--; }

    --
    TB @ SWEDEN
    TB, Jul 4, 2006
    #3
  4. Protoman

    Kai-Uwe Bux Guest

    Protoman wrote:

    > template<class T>
    > class SmrtPtr
    > {
    > public:
    > explicit SmrtPtr(T* obj):ptr(obj)
    > {
    > DataBase.add();
    > for(;;)
    > {
    > if(isInvalid())
    > delete this;
    > }
    > }
    > SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.obj){DataBase.add()}
    > ~SmrtPtr(){delete ptr; DataBase.sub()}
    > T& operator*(){return *ptr;}
    > T* operator->(){return ptr;}
    > T** operator&(){return &ptr;}
    > private:
    > static SmrtPtrDB<T> DataBase;
    > bool isInvalid()
    > {
    > for(;;)
    > if(!DataBase.status())
    > return true;
    > else return false;
    > }
    > T* ptr;
    > };
    >


    Is there a particular reason that you do not use whitespace?

    I corrected several minor typos. Your error message arises since you use
    SmrtPtrDB before defining it. Put both classes in the same file:

    template<class T>
    class SmrtPtrDB{
    public:

    SmrtPtrDB () :num (0) {}

    ~SmrtPtrDB () {}

    void add() { num++; }

    void sub() { num--; }

    int status() { return num; }

    private:

    int num;

    };



    template<class T>
    class SmrtPtr {
    public:

    explicit SmrtPtr ( T* obj )
    : ptr ( obj )
    {
    DataBase.add() ;
    for (;;) {
    if ( isInvalid() ) {
    delete this;
    }
    }
    /*
    What is this loop supposed to accomplish? Why would it terminate?
    */
    }

    SmrtPtr ( const SmrtPtr<T>& rhs)
    :ptr (rhs.obj)
    {
    DataBase.add();
    }

    ~SmrtPtr() {
    delete ptr;
    DataBase.sub();
    }

    T& operator*() { return *ptr; }

    T* operator->() { return ptr; }

    T** operator&() { return &ptr; }

    private:

    static SmrtPtrDB<T> DataBase;
    /*
    static? Why do you want to have one counter per type. One would expect a
    counter per object.
    */
    bool isInvalid()
    {
    for (;;) {
    if (!DataBase.status() ) {
    return true;
    } else {
    return false;
    }
    }
    /*
    This loop will never loop more than once.
    */
    }

    T* ptr;

    };


    What is this smart-pointer class supposed to accomplish?



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jul 4, 2006
    #4
  5. Protoman

    Earl Purple Guest

    Protoman wrote:
    > Here's a non intrusive reference counting smart pointer class I'm
    > working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
    > declaration of `SmrtPtrDB' with no type" error.
    >
    > Code:
    >
    > SmrtPtr.hpp
    >
    > #pragma once


    non-standard pragma.

    > template<class T>
    > class SmrtPtr
    > {
    > public:
    > explicit SmrtPtr(T* obj):ptr(obj)


    reasonable so far although most smart-pointers have an implicit
    constructor from the pointer type. Allows you to do this:

    SmrtPtr< T > getT()
    {
    return new T( params );
    }

    > {
    > DataBase.add();


    > for(;;) // never ending loop, there is no "break"
    > {
    > if(isInvalid())



    > delete this;
    > }


    delete this can be used only on classes created on the heap (i.e. with
    new). Most smart pointers are created on the stack. Self-deletion would
    be undefined. Note that this line will not cause loop termination.

    > }
    > SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.obj){DataBase.add()}
    > ~SmrtPtr(){delete ptr; DataBase.sub()}


    > T& operator*(){return *ptr;}
    > T* operator->(){return ptr;}


    These two should possibly be const functions. Not that they will return
    pointers to const. (If you want that you use SmrtPtr< const T >) but
    because it allows you to use these on temporaries.

    > T** operator&(){return &ptr;}

    very unusual to overload this.

    > private:
    > static SmrtPtrDB<T> DataBase;


    There will be a DataBase for each type T, not for each object being
    pointed to.

    > bool isInvalid()


    another non-const function that probably should be const.

    > {
    > for(;;)


    this loop at least will end beacuse you return in the middle.

    > if(!DataBase.status())
    > return true;
    > else return false;
    > }


    If you are going to test a boolean condition then return the result
    directly, thus:

    return !Database.status();

    > T* ptr;
    > };
    >
    >
    > SmrtPtrDB.hpp
    >
    > #pragma once
    > #include "SmrtPtr.hpp"
    >
    > template<class T>
    > class SmrtPtrDB
    > {
    > public:
    > SmrtPtrDB():num(0){}
    > ~SmrtPtrDB(){}
    > void add(){num++;}
    > void sub(){num--);
    > int status(){return num;}
    > private:
    > int num;
    > };


    > Could you help me out on this? I'm not even sure if I'm coding the
    > non-intrusive reference counting correctly. Thanks!!!!!


    But you're reference counting the wrong thing. If you're not actually
    going to use tr1::shared_ptr / boost::shared_ptr or Loki then at least
    look up the source for boost or Loki to see how it's done. If their
    code in places looks rather complex, that is because writing a good
    non-intrusive smart-pointer is not as trivial as it first seems.
    (Actually some of the complexity in boost comes from sharing code with
    other types of smart-pointer. Much of the complexity also comes from
    custom-deleters, automatic type-conversion and portability across
    libraries).
    Earl Purple, Jul 4, 2006
    #5
  6. Protoman

    Protoman Guest

    Earl Purple wrote:
    > Protoman wrote:
    > > Here's a non intrusive reference counting smart pointer class I'm
    > > working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
    > > declaration of `SmrtPtrDB' with no type" error.
    > >
    > > Code:
    > >
    > > SmrtPtr.hpp
    > >
    > > #pragma once

    >
    > non-standard pragma.
    >
    > > template<class T>
    > > class SmrtPtr
    > > {
    > > public:
    > > explicit SmrtPtr(T* obj):ptr(obj)

    >
    > reasonable so far although most smart-pointers have an implicit
    > constructor from the pointer type. Allows you to do this:
    >
    > SmrtPtr< T > getT()
    > {
    > return new T( params );
    > }
    >
    > > {
    > > DataBase.add();

    >
    > > for(;;) // never ending loop, there is no "break"
    > > {
    > > if(isInvalid())

    >
    >
    > > delete this;
    > > }

    >
    > delete this can be used only on classes created on the heap (i.e. with
    > new). Most smart pointers are created on the stack. Self-deletion would
    > be undefined. Note that this line will not cause loop termination.
    >
    > > }
    > > SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.obj){DataBase.add()}
    > > ~SmrtPtr(){delete ptr; DataBase.sub()}

    >
    > > T& operator*(){return *ptr;}
    > > T* operator->(){return ptr;}

    >
    > These two should possibly be const functions. Not that they will return
    > pointers to const. (If you want that you use SmrtPtr< const T >) but
    > because it allows you to use these on temporaries.
    >
    > > T** operator&(){return &ptr;}

    > very unusual to overload this.
    >
    > > private:
    > > static SmrtPtrDB<T> DataBase;

    >
    > There will be a DataBase for each type T, not for each object being
    > pointed to.
    >
    > > bool isInvalid()

    >
    > another non-const function that probably should be const.
    >
    > > {
    > > for(;;)

    >
    > this loop at least will end beacuse you return in the middle.
    >
    > > if(!DataBase.status())
    > > return true;
    > > else return false;
    > > }

    >
    > If you are going to test a boolean condition then return the result
    > directly, thus:
    >
    > return !Database.status();
    >
    > > T* ptr;
    > > };
    > >
    > >
    > > SmrtPtrDB.hpp
    > >
    > > #pragma once
    > > #include "SmrtPtr.hpp"
    > >
    > > template<class T>
    > > class SmrtPtrDB
    > > {
    > > public:
    > > SmrtPtrDB():num(0){}
    > > ~SmrtPtrDB(){}
    > > void add(){num++;}
    > > void sub(){num--);
    > > int status(){return num;}
    > > private:
    > > int num;
    > > };

    >
    > > Could you help me out on this? I'm not even sure if I'm coding the
    > > non-intrusive reference counting correctly. Thanks!!!!!

    >
    > But you're reference counting the wrong thing. If you're not actually
    > going to use tr1::shared_ptr / boost::shared_ptr or Loki then at least
    > look up the source for boost or Loki to see how it's done. If their
    > code in places looks rather complex, that is because writing a good
    > non-intrusive smart-pointer is not as trivial as it first seems.
    > (Actually some of the complexity in boost comes from sharing code with
    > other types of smart-pointer. Much of the complexity also comes from
    > custom-deleters, automatic type-conversion and portability across
    > libraries).


    OK, now I'm getting errors like:

    4 C:\Dev-Cpp\9.cpp expected nested-name-specifier before "namespace"
    6 C:\Dev-Cpp\SmrtPtrDB.hpp class `SmrtPtrDB' does not have any field
    named `num'
    8 C:\Dev-Cpp\SmrtPtrDB.hpp `num' undeclared (first use this function)

    Here's the code:

    SmrtPtr.hpp

    #pragma once
    #include "SmrtPtrDB.hpp"

    template<class T>
    class SmrtPtr
    {
    public:
    explicit SmrtPtr(T* obj):ptr(obj)
    {
    DataBase.add();
    for(;;)
    {
    if(isInvalid())
    {
    this->~SmrtPtr();
    break;
    }
    }
    }
    SmrtPtr(const SmrtPtr<T>& rhs):ptr(rhs.obj){DataBase.add()}
    ~SmrtPtr(){delete ptr; DataBase.sub()}
    T& operator*()const{return *ptr;}
    T* operator->()const{return ptr;}
    T** operator&()const{return &ptr;}
    private:
    static SmrtPtrDB DataBase;
    bool isInvalid()const
    {
    for(;;)
    return!DataBase.status();
    }
    T* ptr;
    };

    SmrtPtrDB.hpp

    #pragma once

    class SmrtPtrDB
    {
    public:
    SmrtPtrDB():num(0){}
    ~SmrtPtrDB(){}
    void add(){num++;}
    void sub(){num--);
    int status(){return num;}
    private:
    int num;
    };

    9.cpp //main

    #include <iostream>
    #include <cstdlib>
    #include "SmrtPtr.hpp"
    using namespace std;

    int main()
    {
    SmrtPtr<int> ptr(new int);
    SmrtPtr<int> ptr2(ptr);
    delete ptr2;
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    I have no idea what's the problem now. Thanks!!!!
    Protoman, Jul 5, 2006
    #6
  7. Protoman

    Ian Collins Guest

    Protoman wrote:
    > Earl Purple wrote:
    >
    >>Protoman wrote:
    >>
    >>>Here's a non intrusive reference counting smart pointer class I'm
    >>>working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids
    >>>declaration of `SmrtPtrDB' with no type" error.
    >>>


    >
    > I have no idea what's the problem now. Thanks!!!!
    >

    You use non-standard pragmas, omit whitespace and don't fix the typos
    identified in previous responses?

    --
    Ian Collins.
    Ian Collins, Jul 5, 2006
    #7
  8. Protoman

    Earl Purple Guest

    Protoman wrote:
    > template<class T>
    > class SmrtPtr
    > {
    > public:
    > explicit SmrtPtr(T* obj):ptr(obj)
    > {
    > DataBase.add();
    > for(;;)
    > {
    > if(isInvalid())
    > {
    > this->~SmrtPtr();
    > break;
    > }
    > }
    > }


    You should only explicitly call a destructor when you have constructed
    with placement new. Why should your smart-pointer have been constructed
    this way?

    You haven't really fixed your problem.

    Just look at boost and loki to see how to write smart-pointers. And
    then only write your own if you really need something that boost and
    loki don't already support.
    Earl Purple, Jul 5, 2006
    #8
  9. Protoman

    Protoman Guest

    Earl Purple wrote:
    > Protoman wrote:
    > > template<class T>
    > > class SmrtPtr
    > > {
    > > public:
    > > explicit SmrtPtr(T* obj):ptr(obj)
    > > {
    > > DataBase.add();
    > > for(;;)
    > > {
    > > if(isInvalid())
    > > {
    > > this->~SmrtPtr();
    > > break;
    > > }
    > > }
    > > }

    >
    > You should only explicitly call a destructor when you have constructed
    > with placement new. Why should your smart-pointer have been constructed
    > this way?
    >
    > You haven't really fixed your problem.
    >
    > Just look at boost and loki to see how to write smart-pointers. And
    > then only write your own if you really need something that boost and
    > loki don't already support.


    I'm writing this for the learning experience, not b/c I need it.
    Protoman, Jul 5, 2006
    #9
  10. Protoman posted:


    >> Just look at boost and loki to see how to write smart-pointers. And
    >> then only write your own if you really need something that boost and
    >> loki don't already support.

    >
    > I'm writing this for the learning experience, not b/c I need it.



    With that attitude you'll become a very proficient programmer indeed.


    --

    Frederick Gotham
    Frederick Gotham, Jul 5, 2006
    #10
  11. Protoman

    Protoman Guest

    Frederick Gotham wrote:
    > Protoman posted:
    >
    >
    > >> Just look at boost and loki to see how to write smart-pointers. And
    > >> then only write your own if you really need something that boost and
    > >> loki don't already support.

    > >
    > > I'm writing this for the learning experience, not b/c I need it.

    >
    >
    > With that attitude you'll become a very proficient programmer indeed.
    >
    >
    > --
    >
    > Frederick Gotham


    OK, where can I GET boost and loki?
    Protoman, Jul 5, 2006
    #11
  12. * Protoman:
    >
    > OK, where can I GET boost and loki?


    Protoman, I give you... WIKIPEDIA!

    <url: http://en.wikipedia.org/wiki/Boost_library>
    <url: http://en.wikipedia.org/wiki/Loki_%28C%2B%2B%29>

    Of course there's also YAHOO, GOOGLE, ALLTHEWEB, LYCOS, ALTAVISTA, etc.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 5, 2006
    #12
  13. Frederick Gotham, Jul 5, 2006
    #13
  14. Alf P. Steinbach schrieb:
    > * Protoman:
    >>
    >> OK, where can I GET boost and loki?

    >
    > Protoman, I give you... WIKIPEDIA!
    >
    > <url: http://en.wikipedia.org/wiki/Boost_library>
    > <url: http://en.wikipedia.org/wiki/Loki_%28C%2B%2B%29>
    >
    > Of course there's also YAHOO, GOOGLE, ALLTHEWEB, LYCOS, ALTAVISTA, etc.


    Google? Why should he know that you can find things with google? He uses
    google for news. Thats the only purpose, isn't it? :)

    --
    Thomas
    Thomas J. Gritzan, Jul 5, 2006
    #14
    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. Smart Pointer help

    , May 25, 2005, in forum: C++
    Replies:
    8
    Views:
    1,517
    Pete Becker
    May 25, 2005
  2. MotoK
    Replies:
    59
    Views:
    1,809
    Keith Thompson
    Sep 15, 2006
  3. coala
    Replies:
    3
    Views:
    373
    coala
    Sep 6, 2006
  4. coala
    Replies:
    1
    Views:
    590
    Victor Bazarov
    Sep 6, 2006
  5. Hicham Mouline
    Replies:
    100
    Views:
    2,081
    Noah Roberts
    Aug 25, 2009
Loading...

Share This Page