Please explain the following code in detail [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 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.

Related

what is npos ??? My problem is I do not understand why anyone would use it [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 2 years ago.
Improve this question
What is npos ??? I recently saw it in some source code and really want to know what it is used for and why it is used. An example would be nice also. Thanks(:
The documentation says :
npos is a static member constant value with the greatest possible
value for an element of type size_t
This value, when used as the value for a len (or sublen) parameter
in string's member functions, means "until the end of the string".
As a return value, it is usually used to indicate no matches.
It is defined internally as :
static const size_t npos = -1;
Just remember, it's used to indicate not found.

C++ references and modification [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 5 years ago.
Improve this question
Lets say I have
void swap(int &x, int &y){
//do swap here
}
Why is it legal to do: x = y inside of the function so that it assigns the VALUE of y to x?
Why isn't some sort of dereferencing needed?
There's no dereferencing needed since references are not pointers. They are aliases to the referenced object.

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.