How can an immutable string be implemented in C++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
All my attempts so far have failed.Basically when I return a copy of the internal char array of the string, that copy has to be released, but I don't know how to release it.Wrapping it in a smart pointer doesn't work out, since it's destructor gets called immediately after I return it.Must I implement something like a garbage collector just for the immutable string?

const std::string will be fine.

Related

Move constructor with vector of pointers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How should i do the move constructor for a vector with pointers?
for example :
I have this in class A as a field - vector<A*> AList;
In the function (A &&otherA) ( which is the move constructor) ,
should i write like this :
AList(std::move(other.AList)) or in an other way?
Yes, you can do it like that.
Or, if you don't need the move assignment operator to do anything else, just leave it out and the compiler will do this for you.
This goes for moving any member vector.

Are "<Dummy>" and "<Null>" keywords? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
static const string FindSentinel() { return "<Dummy>"; }
static const string Void() { return "<Null>"; }
What does this do, and how can these functions be used?
Is "<Dummy>" and "<Null>" a keyword?
No. They are plain string literals.
The calling code may assign special meaning to those strings (if it wants to), but as far as the C++ language is concerned, they are just any old run-of-the-mill string literal - nothing to see here, move along.

Why is const char INITIAL='G' not an assignment statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am having problems in understanding this statement. I don't know why this is not usual like others.
Assignment means giving a new value to an already existing object. Even though const char INITIAL='G'; has an = sign, it is not an assignment, because it is creating a new object, not modifying an existing one. char INITIAL; INITIAL='G'; would be an assignment, because INITIAL already exists when the new value is, well, assigned.

How likely is istream::ungetc() to work with a stringbuf (as used in stringstream)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've tried searching for the answer, but nothing mentions stringstream specifically. I would guess that it would always work and you can always go back as far as the beginning of the underlying string.
Am I right?
How likely is istream::ungetc() to work with a stringbuf (as used in stringstream)?
Well, never.
There's no such thing like istream::ungetc() defined from the standard.
You can use either
int std::ungetc( int ch, std::FILE *stream )
or
std::basic_istream& std::basic_istream::unget()

potential issue in this code using osstreamstring [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any issue in the following code? I am told it's there, but I couldn't find it...
std::string fun(int i)
{
std::ostringstream t;
t<<"My int is "<<i<<returnSomething();
return t.str();
}
The code in the question is fine. It would be a problem if you returned a pointer into the local object (say that you returned a const char* obtained as t.str().c_str()) or if you returned a reference. But in your code, a copy of the internal string in the std::ostringstream is performed before the function completes (as part of the return statement), and before t gets destroyed, so it is fine.