C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem while making a string_proxy

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Frank Yeh
Guest





PostPosted: Sun Jul 25, 2004 1:35 am    Post subject: Problem while making a string_proxy Reply with quote



Hi All,
I designed a stl-compatible container(wrapper) for TStrings
(TStrings is a structure looks like an array of strings).
The container defined an iterator - "tstrings_iterator", and
tstrings_iterator::operator*() returns a "string_proxy".
The "string_proxy" works as a reference to the source string,
and it contains a pointer to the source object and an index to
the refered string.
It works like this....
//////////////////////////////////////////////////////////////////////
void funtion(void)
{
tstrings_iterator itor1,itor2;
....
....
*itor1 = "Writing";
// 1st call-->string_proxy tstrings_iterator::operator*() const
// {Returns a temporary string_proxy}
// 2nd call--->void string_proxy::operator=(const char* rhs)
// {Writes to the source string}


*itor2 = *itor;
// *itor1 and *itor2 both return temporary "string_proxy"
// and the code becomes --->string_proxy_1 = string_proxy_2;
}

//////////////////////////////////////////////////////////////////////////

Everything worked fine until one day I meet a problem....

//////////////////////////////////////////////////////////////////////////

//This is a part of stl source from Borland C++ Builder 6.0
//
// _RandomAccessIter --> regarded as "tstrings_iterator"
// _Tp --> The value type of RandomAccessIterator
// its type is "string_proxy"

template <class _RandomAccessIter, class _Tp, class _Compare>
inline void __linear_insert(_RandomAccessIter __first,
_RandomAccessIter __last,
_Tp __val, _Compare __comp)
{

if (__comp(__val, *__first))
{
copy_backward(__first, __last, __last + 1);
*__first = __val;
}
...
...
}

//////////////////--Here is the problem Sad
////////////////////////////////

The parameter "_Tp __val" should works as a non-reference value type.
Therefore, after calling the copy_backward(...), the __val should NOT
change.
However, "string_proxy" works like as a reference to the source string
, and it produces a bug....

{
copy_backward(__first, __last, __last + 1);
*__first = __val; // oh my God... __val is changed!!
}

Is there anyway to solve this problem?? (instead of give up
using Borland's product...)

Please give me some suggestions. Thanks Advanced...

/////////--The implementation of string_proxy is as
follows--/////////////

class stl_tstrings;
template<typename string_proxy>
class tstring_iterator;

class ansistring_proxy{

friend class stl_tstrings;
template<typename string_proxy>
friend class tstring_iterator;

protected:

int index_;
TStrings* source_;

protected:

ansistring_proxy(TStrings* source,int index):
index_(index),source_(source){;}
explicit ansistring_proxy(const ansistring_proxy& rhs):

index_(rhs.index_),source_(rhs.source_){;}

public:
void operator=(const ansistring_proxy& rhs)
{
source_->Strings[index_] =
rhs.source_->Strings[rhs.index_];
}

void operator=(const AnsiString& rhs)
{
source_->Strings[index_] = rhs;
}

operator AnsiString() const{return
source_->Strings[index_];}

...
...

};


Best Regards
Frank

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.