Tuesday 27 March 2018

c++ - Do function parameter variables always require a & or * operator?

I'm reteaching myself C++ and as I'm reading about pointers a question has came into mind.



When declaring function signatures in C++, the address-of and dereference operators are used, such as the below.



int someFunction(std::vector& nums) {
//do stuff
}



In this context the & is being used to declare that the address of the nums variable is being used rather than the value.



If this signature is changed to the below, the value is being used instead.



int someFunction(std::vector* nums) {
//do stuff
}



However if the below is now used, I assume the value of the nums variable is still being used despite the lack of operator.



int someFunction(std::vector nums) {
//do stuff
}


If this is true, since the lack of an operator and the * operator both result in the same thing, why is there any need for the * operator at all? Is it simply for brevity?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...