I am learning c++ and trying to fully understand the deference between if I take an argument like this (const type& var)
versus (type var)
. I my understanding that by passing the "reference" (const type& var)
will cost less because doesn't require a "copy constructer" or "destroy constructer" which make sense. But:
- when I should use this because in some cases
(type var)
cost less. when I should use this because in some cases(type var)
cost less. - why we not use pointer instead?
(beginner question) how can we pass "value" to a "reference"?
class Human{ private: friend void DisplayAge (const Human& Person); string name; int age; public: Human (string personName, int personAge): name(personName), age(personAge) {} }; void DisplayAge(const Human& Person) { cout << Person.age << endl; } int main() { Human FirstMan("Adam", 25); cout << "Accessing private member age via friend function: "; DisplayAge(FirstMan); return 0; }