i'm writting some kind of intelligent pointer
suppose user put const Class as typename T.
how to remove const part of T?

Recommended Answers

All 3 Replies

>suppose user put const Class as typename T.
Then you should respect their wishes, duh. A smart pointer doesn't sound very smart if it doesn't know how to properly point to a const type...

hm, what i'm thinking about is that user should be able to do
ptr<Csmth> a;
ptr<const Csmth>b = a;
this is intuitive with normal pointers

Oh good, at least you have a legitimate reason. But don't fight const, work with it. For example, instead of stripping away const at every turn, provide an implicit conversion from non-const to const:

template <typename T>
class foo_ptr {
  T *_base;
public:
  foo_ptr ( T* init = 0 )
    : _base ( init )
  {}

  template <typename U>
  operator foo_ptr<U>()
  {
    return foo_ptr<U> ( _base );
  }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.