G
Gianni Mariani
Jess said:Hello,
I'm doing an exercise that defines a new abstract class "Str" that has
the same functions as "string" class, which holds a vector of chars.
But since I'm also trying to define a "c_str()" function, which copies
the chars from the vector into an array and return the pointer, I
think I need add another char array to my "Str" class. Now, the
problem is the size of the array has to be known in advance, but
obviously, the array's size changes when the corresponding
vector<char> changes. Any suggestions on how I should implement the
c_str() function?
c_str() simply needs to ensure that the character at one beyond the end
of the string contains a null. (i.e. '\000').
One way is to always allocate one more char than is needed and when
c_str() is called, plonk a nul at the end and return a pointer to your
internal buffer. This is probably the simplest solution.
Other solutions involve creating an auxiliary buffer as you noted.