C++ references and modification [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
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.

Related

Cast vector to iterator? [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 yesterday.
Improve this question
I have the following code
vector::vector(const vector& arg)
:sz{arg.sz}, elem{new double[arg.sz]}
{
copy(arg, arg+sz, elem);
}
How is arg converted into an iterator here? whats the mechanism called that defines the conversion internally of vector outputting the needed iterator?
Thanks

how to insert a char* into the end of a vector<char>? [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 1 year ago.
Improve this question
vector<char> vec;
vec.push_back(0x1);
char* a = "qwe";
I want to push a to the end of the vector.
You can use the .insert() member function with pointers standing in as iterators.
vec.insert(vec.end(), a, a+strlen(a));

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.

Return a pointer to pointers in a function [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 7 years ago.
Improve this question
I have a matrix in one class. That matrix is allocated dinamically, I want to encapsulate it.
Here is my matrix declaration in the Header file:
float** matrix;
And here is the declaration of get method:
float *getMatrix();
Is everything correct so far?
I don't know how to work with pointers in that case. How would the get function look like?
if you want just to return matrix you should use
float** getMatrix();

How can an immutable string be implemented in C++? [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
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.