Tuesday, March 27, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...