Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > easy conversion between two basic_string types...

Reply
Thread Tools

easy conversion between two basic_string types...

 
 
Jim Kogler
Guest
Posts: n/a
 
      08-02-2004
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?

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...
return *this;
}

of course the same question could apply to the copyCtor too...

and, to go the other way, should i do the same thing for the

operator const std::string&() {...} method?

Is there a better way to create my type?

Jim
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-02-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
Initializing std::basic_string<> with literals Dylan Nicholson C++ 14 02-17-2004 11:35 AM
STL & basic_string<XMLCh> link error =?ISO-8859-15?Q?R=E9mi_Peyronnet?= C++ 0 02-16-2004 10:03 PM
STL basic_string: MBS -> wide conversion Jin C++ 0 12-24-2003 01:31 PM
unit test for basic_string? Paul Pedriana C++ 1 11-01-2003 05:32 AM



Advertisments