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.
Related
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 5 years ago.
Improve this question
const int m=10;
int &n=m;
n=11;
cout << m << n;
It results in compile time error.
I am just unable to learn this pointer variable. Please explain.
I can see why you're having trouble with this pointer variable: it isn't one. It is a reference.
You cannot have a non-const reference to a const thing. That would violate the const, and allow the n = 11 line to succeed, thus again violating the original const.
const means "I cannot and will not change this thing any more".
For more information, turn to the page in your C++ book about const.
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.
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()
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.
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
Currently, I have code like:
static YAML::Node *doc;
...
__attribute__((constructor)) void inityaml() {
doc = new YAML::Node;
parser.GetNextDocument(*doc);
}
The question is, is there any more C++-conventions-ish way to perform this task, like the use of a global reference or something?
Why not avoid heap allocation altogether?
i.e.
static YAML::Node doc;
...
void inityaml() {
parser.GetNextDocument(&doc);
}