c++ - Constness problems in c_str() function in custom string implementation -
Still new to C ++ I'm trying to implement the c_str () function I tried:
class str {public: typedef size_t size_type; Typingfaces Four * Iterator; Type-tof cone four * constitutor; Const char * c_str () const {char * tmp = alloc.allocate (length + 1); Alloc.construct (TMP + length + 1, '\ 0'); For (size_type i = 0; i But my compiler is complaining. It seems that, due to the work defined as Const, allocation () must be returned to the pointpoint (or something wrong happened to me). Anyway, this is not working and I want to know what is the matter.
1) I know that I call allocated (), but I call it on new variable, this is the reason I do not get that part.
2) I know about "Return Data", but I wanted to know why this would not work. In C ++ 11, there is no difference between c_str () and data (), but we still say this: If I always "return data" how do I make a difference between them?
the way c_str () std :: string < / Code> has a reserved space for an additional character in data and it is terminated continuously at all times, so that c_str () simply returns the return data; and it is already guaranteed to become redundant.
It seems that the allocation of functions defined as const () should be given to the parent point (or something wrong) to me.
No, you definitely have something wrong.
The problem with your code is that alloc occurs in that member function, you can resolve it by creating a non-static copy of the allocator:
< Code> const char * c_str () const {std :: allocator & lt; Char & gt; Alloc2 (alloc); Four * tmp = alloc2.allocate (length + 1); Alloc2.construct (TMP + length + 1, '\ 0'); For (size_type i = 0; i This function is not correct, because whenever you make a call, it allocates memory, so call it the std :: allocator & lt; Char & gt; , do not no c_str () ! The correct interface for
is std :: allocator & lt; Char & gt; Using :: build is absolutely useless, you can also tmp [length] = '\ 0' . Even std :: allocator & lt; Char & gt; is too meaningless to use, you can simply use new and delete [] . Due to the use of an allocation, users have to allow it to be customized with optional alcoters, but if you only want it to be std :: allocator & lt; Char & gt; If you are going to fix to use, then there is no real advantage to use it, an allottee is at all, and you simply store your class in vain for alloc To take more space.
Also, this tmp + length ?
alloc.construct (tmp + length + 1, '\ 0'); Respond to edited queries :
1) I know that I call the allocation (), but I call it new variable, not a member of Str, that is why I do not get that part.
Erm, no, you do not call it on any member!
2) I know about "return data", but I wanted to know why this would not work. In C ++ 11, there is no difference between c_str () and data (), but we still say: if I always have "Return Data"; "
C + 03 03 Do not say that there is a difference between you and me, so how do I make a difference between them? should should be the difference between the two, it is favorable for them to be equal, and one of the reasons
c_str ()anddata ()Absolutely in C ++ 11 is similar to how all the implementation was done in C ++ 03 this is actually the only way to do this There is a way.Separation of them for no reason just makes it very difficult to implement
c_str ()and this is also a very stupid design option. / P >
Comments
Post a Comment