DynPtr: A pointer with implicit dynamic cast

Discussion in 'C++' started by Calum Grant, Dec 6, 2005.

  1. Calum Grant

    Calum Grant Guest

    Here's a little utility I thought I'd share.

    (For those familiar with ATL::CComQIPtr, the concept is similar).


    ====

    #ifndef DYNPTR_H_INCLUDED
    #define DYNPTR_H_INCLUDED

    /*
    DynPtr - A pointer with an implicit dynamic-cast

    Purpose: When code requires a lot of dynamic_casting,
    it can be difficult to read, so DynPtr is a pointer
    that implicitly
    dynamic_casts, to save the bother of writing dynamic_cast all
    the time.

    e.g.

    Integer *i = dynamic_cast<Integer*>(value);
    Integer *ci = dynamic_cast<const Integer*>(value);

    vs

    DynPtr<Integer> i(value);
    DynPtr<const Integer> ci(value);

    Written by Calum Grant. http://visula.org/calum
    Public domain - free of copyright.
    */

    template<class T>
    class DynPtr
    {
    T *p;
    public:
    DynPtr() : p(0) { }

    DynPtr(const DynPtr &q) : p(q.get()) { }
    explicit DynPtr(T *q) : p(q) { }

    template<class Q>
    explicit DynPtr(const DynPtr<Q> &q) : p(dynamic_cast<T*>(q.get())) { }

    template<class Q>
    explicit DynPtr(Q *q) : p(dynamic_cast<T*>(q)) { }

    DynPtr &operator=(const DynPtr &q) { p = q.get(); return *this; }
    DynPtr &operator=(T *p) { p = q; return *this; }

    template<class Q> DynPtr<T> &operator=(const DynPtr<Q> &q)
    {
    p = dynamic_cast<T*>(q.get());
    return *this;
    }

    template<class Q> DynPtr<T> &operator=(Q *q)
    {
    p = dynamic_cast<T*>(q);
    return *this;
    }

    operator bool() const { return p!=0; }
    T *operator->() const { return p; }
    T &operator*() const { return p; }
    T *get() const { return p; }
    };

    #endif
    Calum Grant, Dec 6, 2005
    #1
    1. Advertising

  2. Calum Grant

    Guest

    Calum Grant wrote:
    > template<class T>
    > class DynPtr
    > {
    > T *p;
    > public:
    > DynPtr() : p(0) { }
    >
    > DynPtr(const DynPtr &q) : p(q.get()) { }
    > explicit DynPtr(T *q) : p(q) { }
    >
    > template<class Q>
    > explicit DynPtr(const DynPtr<Q> &q) : p(dynamic_cast<T*>(q.get())) { }
    >
    > template<class Q>
    > explicit DynPtr(Q *q) : p(dynamic_cast<T*>(q)) { }
    >
    > DynPtr &operator=(const DynPtr &q) { p = q.get(); return *this; }
    > DynPtr &operator=(T *p) { p = q; return *this; }
    >
    > template<class Q> DynPtr<T> &operator=(const DynPtr<Q> &q)
    > {
    > p = dynamic_cast<T*>(q.get());
    > return *this;
    > }
    >
    > template<class Q> DynPtr<T> &operator=(Q *q)
    > {
    > p = dynamic_cast<T*>(q);
    > return *this;
    > }
    >
    > operator bool() const { return p!=0; }
    > T *operator->() const { return p; }
    > T &operator*() const { return p; }


    This doesn't look like it should compile.
    Did you mean 'return *p;' maybe?

    > T *get() const { return p; }
    > };
    >
    > #endif


    Cheers,
    Andre
    , Dec 6, 2005
    #2
    1. Advertising

  3. Calum Grant wrote:

    >
    > /*
    > DynPtr - A pointer with an implicit dynamic-cast
    >
    > Purpose: When code requires a lot of dynamic_casting,


    IMHO, one should reconsider the design.
    Neelesh Bodas, Dec 7, 2005
    #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. Replies:
    7
    Views:
    419
  2. Flo
    Replies:
    2
    Views:
    592
    Rolf Magnus
    Jul 18, 2006
  3. arnuld

    malloc() and implicit cast

    arnuld, Apr 15, 2008, in forum: C Programming
    Replies:
    7
    Views:
    870
    Antoninus Twink
    Apr 16, 2008
  4. Replies:
    2
    Views:
    360
    red floyd
    Nov 26, 2008
  5. Pavel
    Replies:
    7
    Views:
    507
    Pavel
    Sep 19, 2010
Loading...

Share This Page