【发布时间】:2019-10-08 20:50:36
【问题描述】:
我正在学习 c++,并试图完全理解如果我接受这样的论点 (const type& var) 与 (type var) 之间的区别。我的理解是,通过“参考”(const type& var) 将花费更少,因为不需要有意义的“复制构造函数”或“销毁构造函数”。但是:
- 什么时候我应该使用它,因为在某些情况下
(type var)成本更低。 我什么时候应该使用它,因为在某些情况下(type var)成本更低。 - 为什么我们不用指针来代替?
-
(初学者问题)我们如何将“值”传递给“引用”?
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; }
【问题讨论】: