I've been learning Java for about four months now and a few days ago I purchased some books on C++. (currently reading C++ Primer Plus) Initially a few things threw me off (like how C++ passes objects by value and instead of passing references by value), but either I'm a slow learner or simply not understanding this next concept correctly.
Please correct me if I am wrong on any of these statements. I have a headache and my brain is fried; I will probably say something stupid.
I understand that using const at the end of a member function like void method() const allows a const object to use that function. A method with a const parameter prevents that method from modifying any data in that object (ex void method(const Parameter1 & prm).
My confusion begins when you use const and references.
I don't think I understand how returning references works.
I know that
int a = 5;
int & b = a; (b "points" to the value at the address of a)
is just about equivalent to:
int a =5;
int * b = &a;(b points to the address of a)
but what exactly happens step by step when you return a reference? For example let's say the function is int & getIntRef(int & a){return a;}. I could use getIntRef(a) = 5 to change the value at that memory address.
I know what the end result is, but what is going on at a deeper level? Is there such a thing as "copying" references or am I thinking about it the wrong way?
When const is thrown into the mix I get even more confused. If I reuse the previous function and add const in front like so: const int & getIntRef(int & a){return a;} I know a few things. I can't type something like getIntRef(a) = 5. I can type int b = getIntRef(a). Does the const mean the reference is const (which seems redundant since you cannot reassign references) or the value it is pointing to become const? Wouldn't that mean I cannot modify the original "int a" that was passed anymore (even through other methods or directly)? Or I just can't modify the value through that specific returned reference?
I *think* I understand pointers more right now, what would be the equivalent if written with pointers?
Sorry for the wall of text or any stupid questions.
Please correct me if I am wrong on any of these statements. I have a headache and my brain is fried; I will probably say something stupid.
I understand that using const at the end of a member function like void method() const allows a const object to use that function. A method with a const parameter prevents that method from modifying any data in that object (ex void method(const Parameter1 & prm).
My confusion begins when you use const and references.
I don't think I understand how returning references works.
I know that
int a = 5;
int & b = a; (b "points" to the value at the address of a)
is just about equivalent to:
int a =5;
int * b = &a;(b points to the address of a)
but what exactly happens step by step when you return a reference? For example let's say the function is int & getIntRef(int & a){return a;}. I could use getIntRef(a) = 5 to change the value at that memory address.
I know what the end result is, but what is going on at a deeper level? Is there such a thing as "copying" references or am I thinking about it the wrong way?
When const is thrown into the mix I get even more confused. If I reuse the previous function and add const in front like so: const int & getIntRef(int & a){return a;} I know a few things. I can't type something like getIntRef(a) = 5. I can type int b = getIntRef(a). Does the const mean the reference is const (which seems redundant since you cannot reassign references) or the value it is pointing to become const? Wouldn't that mean I cannot modify the original "int a" that was passed anymore (even through other methods or directly)? Or I just can't modify the value through that specific returned reference?
I *think* I understand pointers more right now, what would be the equivalent if written with pointers?
Sorry for the wall of text or any stupid questions.