Jim Kogler wrote:
> I have created a new string type with a memory pool allocator. Lets
> presume the allocator works, my type is defined as:
>
>
> typedef std::basic_string<char, std::char_traits<char>,
> pool_allocator<char> > NewStr;
>
> This is the same as std::string except with a different allocator,
> right?
Right. It makes it a totally different, unrelated type, nonetheless.
>
> I want to be able to implicitly convert from std::string to NewStr
> like:
>
> std::string a("hi");
> NewStr b("there");
> a = b;
>
> or
> b = a;
>
> which doesnt work, becasue they are different types. So my question
> is, if i wanted to do this:
>
> class MyString : public NewStr
> { public:
> MyString &operator=(const std::string &other)
> {
> // what is the best way to do this?
> *this = other.c_str(); /// doesnt look good...
Probably something like
this->assign(other.begin(), other.end());
> return *this;
> }
>
> of course the same question could apply to the copyCtor too...
You may use 'assign' there too.
>
> and, to go the other way, should i do the same thing for the
>
> operator const std::string&() {...} method?
No. What would it be a reference to? You can only return an object
from that, and not a reference:
operator std::string() const {
return std::string(this->begin(), this->end());
}
>
> Is there a better way to create my type?
Not if you want your implicit conversions.
Victor
|