easy conversion between two basic_string types...

Discussion in 'C++' started by Jim Kogler, Aug 2, 2004.

  1. Jim Kogler

    Jim Kogler Guest

    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
    Jim Kogler, Aug 2, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 2, 2004
    #2
    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. Paul Pedriana

    unit test for basic_string?

    Paul Pedriana, Oct 18, 2003, in forum: C++
    Replies:
    1
    Views:
    457
    Paul Pedriana
    Nov 1, 2003
  2. Jin
    Replies:
    0
    Views:
    391
  3. Thomas Matthews

    Conversion between string types

    Thomas Matthews, Feb 24, 2004, in forum: C++
    Replies:
    2
    Views:
    3,700
    Thomas Matthews
    Feb 25, 2004
  4. MikeL
    Replies:
    2
    Views:
    261
    Dino Chiesa [Microsoft]
    Feb 22, 2005
  5. Stefan Ram
    Replies:
    1
    Views:
    70
    Victor Bazarov
    Mar 18, 2014
Loading...

Share This Page