I have a class let's say
class Foo { public: void ProcessString(std::string &buffer) {
This class tries to use the std::string buffer to perform operations with it using various methods in these conditions:
- I do not want to pass the copy of
std::string that I already have. - I do not want to create multiple objects of this class.
For example:
I could use a string and assign it as a member of the class so that other functions can know about it.
But it seems that we cannot have a reference class std::string &buffer , because it can only be initialized from the constructor.
I could use a pointer to std::string ie std::string *buffer and use it as a member of the class, and then pass the addresses s1, s2, s3 .
class Foo { public: void ProcessString(std::string *buf) {
Or another way can pass each function a link to the std::string buffer in the same way as shown in the first example above .
Both methods seem to be ugly workarounds, allowing you to use std::string without copying, since I rarely saw std :: string being used as a pointer or passing all the functions of a class to the same argument.
Is there any better in this, or what am I doing, just fine?
c ++ string parameter-passing pass-by-reference stl
cpx
source share