C++类对象作为函数参数传递详解

我们知道了如何使用变量作为函数的实参,类对象也可以作为实参传递给函数

例如,以下函数具有一个接收 Rectangle 对象的形参:

void displayRectangle(Rectangle r)
{
    cout << "Length = " << r.getLength() << endl;
    cout << "Width = " << r.getWidth() << endl;
    cout << "Area = " << r.getArea() << endl;    '
}

以下代码创建了一个长度为 15 和宽度为 10 的 Rectangle 对象 box,然后将 box 对象作为参数传递给了 displayRectangle 函数:

Rectangle box(15, 10);
displayRectangle(box);

假设 Rectangle 类包含本示例中使用的成员函数 displayRectangle,则该函数将输出以下信息:

Length = 15
Width = 10
Area = 150

与常规变量一样,对象可以通过值或引用传递给函数。在 Rectangle 示例中,box 通过值传递给 displayRectangle 函数,这意味着 displayRectangle 函数会收到一个 box 的副本。如果 displayRectangle 调用任何 Rectangle 类设置器函数,那么它们将只会更改 box 的副本,而不是原来的 box。如果函数需要存储或更改对象成员变量中的数据,则必须通过引用将对象传递给它。

下面的程序说明了这一点:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class InventoryItem
  7. {
  8. private:
  9. int partNum; // Part number
  10. string description; // Item description
  11. int onHand; // Units on hand
  12. double price; // Unit price
  13. public:
  14. void storeInfo(int p, string d, int oH, double cost); // Prototype
  15. int getPartNum()
  16. {
  17. return partNum;
  18. }
  19. string getDescription()
  20. {
  21. return description;
  22. }
  23. int getOnHand()
  24. {
  25. return onHand;
  26. }
  27. double getPrice()
  28. {
  29. return price;
  30. }
  31. };
  32.  
  33. void InventoryItem::storeInfo(int p, string d, int oH, double cost) {
  34. partNum = p;
  35. description = d;
  36. onHand = oH;
  37. price = cost;
  38. }
  39.  
  40. //Function prototypes for client program
  41. void storeValues (InventoryItem&);// Receives an object by reference
  42. void showValues (InventoryItem); // Receives an object by value
  43.  
  44. int main()
  45. {
  46. InventoryItem part; // part is an Inventoryltem object
  47. storeValues(part);
  48. showValues(part);
  49. return 0;
  50. }
  51. void storeValues(InventoryItem &item)
  52. {
  53. int partNum; // Local variables to hold user input
  54. string description;
  55. int qty;
  56. double price;
  57. // Get the data from the user
  58. cout << "Enter data for the new part number \n";
  59. cout << "Part number: ";
  60. cin >> partNum;
  61. cout << "Description: ";
  62. cin.get();
  63. getline (cin, description);
  64. cout << "Quantity on hand: ";
  65. cin >> qty;
  66. cout << "Unit price: ";
  67. cin >> price;
  68. item.storeInfo(partNum, description, qty, price);
  69. }
  70. void showValues(InventoryItem item)
  71. {
  72. cout << fixed << showpoint << setprecision(2) << endl;
  73. cout << "Part Number : " << item.getPartNum() << endl;
  74. cout << "Description : " << item.getDescription () << endl;
  75. cout << "Units On Hand: " << item.getOnHand () << endl;
  76. cout << "Price : $" << item.getPrice() << endl;
  77. }
程序输出结果:

Enter data for the new part number
Part number: 175
Description: Hammer
Quantity on hand: 12
Unit price: 7.49

Part Number : 175
Description : Hammer
Units On Hand: 12
Price : $7.49

程序中有两个函数接收 InventoryItem 对象。该对象通过引用传递给 storeValues,因为该函数需要调用一个类设置器函数,将新值存储到对象中。该对象通过值传递给 showValues,因为此函数只需要使用访问器函数来检索和使用存储在对象数据成员中的值。

程序中还可以看到,Inventoryltem 类声明出现在 storeValues 和 showValues 函数的原型之前,这一点很重要,因为这两个函数都有一个 Inventoryltem 对象作为形参,所以编译器在遇到任何引用它的语句之前,必须知道 Inventoryltem 是什么,否则就会发生错误。

常量引用形参

在上面的程序中,InventoryItem 对象按值传递给 showValues 函数。但是,按值传递对象需要复制所有对象成员的副本,这可能会减慢程序的执行时间,如果对象有很多成员,则更是如此。另一方面,当按引用传递对象时,由于该函数可以访问原始对象,而不必进行任何复制,所以它比通过值传递更快,正因为如此,一般更愿意按引用传递对象。

但是,按引用传递对象有一个缺点,因为该函数可以访问原始对象,所以它可以调用其设置器函数更改对象成员数据。这就是为什么当程序员想要保护对象的内容时,通常不会按引用传递变量。

幸运的是这个问题有解决办法。为了保护对象让它作为实参传递,而又不必复制副本,可以将它作为常量引用进行传递,这意味着原始对象作为引用被传递给了函数,但是它不能调用任何设置器函数或更改对象的成员数据,它只能调用自己被指定为常量函数的访问器函数。

要将形参声明为常量引用形参,必须将关键字 const 放在函数原型和函数头的形参列表中。对照上面程序中的 showValues 函数的函数原型和函数头,如果将它改为使用常量引用形参,则语句如下:

void showValues (const InventoryItem&) //函数原型
void showValues (const InventoryItem &item) // 函数头

现在,showValues 函数只能调用 InventoryItem 函数,也可以在函数原型和函数头中列出关键字 const,如下所示:

double getPrice() const

如果 showValues 尝试调用任何其他 InventoryItem 函数,则会发生编译器错误。请注意,当 showValues 被修改为具有常量引用形参时,只有函数原型和函数头被更改为包含关键字 const。showValues 函数的主体和对 showValues 的调用不会改变。

从函数返回一个对象

正如函数可以编写为返回一个 int、double 或其他数据类型一样,它们也可以设计为返回一个对象。

事实上,当以前从函数返回一个字符串时,就已经是在这样做了,因为字符串就是一个对象。当函数返回一个对象时,它通常会创建该类的局部实例,设置其数据成员,然后返回它。

以下仍以上面程序为例,说明如何在 storeValues 函数中创建 InventoryItem 对象,然后返回到调用函数。请注意,这个新版本的 storeValues 函数不接收任何实参,它的返回类型现在是 InventoryItem 而不是 void。
  1. InventoryItem storeValues()
  2. {
  3. InventoryItem templtem; // Inventoryltem 局部对象
  4. int partNum; //存储用户输入的局部变量
  5. string description;
  6. int qty;
  7. double price;
  8. //在此编写获取用户输入的代码.
  9. //将数据保存到InventoryItem对象并返回它
  10. tempItem.storeInfo(partNum, description, qty, price);
  11. return tempItem;
  12. }
main 函数随后应创建 part 语句,如下所示:

InventoryItem part = storeValues();

下面的程序修改了之前的程序,以纳入刚才讨论的技术。以前名为 storeValues 的函数被重命名为 createItem,因为它现在会创建一个名为 InventoryItem 的对象并将其返回给 main。showValues 函数现在接收 part 作为常量引用,而不是像以前一样按值传递它。
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class InventoryItem
  7. {
  8. private:
  9. int partNum; // Part number
  10. string description; // Item description
  11. int onHand; // Units on hand
  12. double price; // Unit price
  13. public:
  14. void storeInfo (int p, string d, int oH, double cost);// Prototype
  15. int getPartNum () const
  16. {
  17. return partNum;
  18. }
  19. string getDescription() const
  20. {
  21. return description;
  22. }
  23. int getOnHand() const
  24. {
  25. return onHand;
  26. }
  27. double getPrice() const
  28. {
  29. return price;
  30. }
  31. };
  32. void InventoryItem::storeInfo(int p, string d, int oH, double cost)
  33. {
  34. partNum = p;
  35. description = d;
  36. onHand = oH;
  37. price = cost;
  38. }
  39. InventoryItem createItem(); // Returns an Inventoryltem object
  40. void showValues (const InventoryItem&);
  41.  
  42. int main()
  43. {
  44. InventoryItem part = createItem ();
  45. showValues(part);
  46. return 0;
  47. }
  48. InventoryItem createItem()
  49. {
  50. InventoryItem tempItem;// Local Inventoryltem object
  51. int partNum; // Local variables to hold user input
  52. string description;
  53. int qty;
  54. double price;
  55. //Get the data from the user
  56. cout << "Enter data for the new part number \n";
  57. cout << "Part number: ";
  58. cin >> partNum;
  59. cout << "Description:";
  60. cin.get();
  61.  
  62. getline(cin, description);
  63. cout << "Quantity on hand: ";
  64. cin >> qty;
  65. cout << "Unit price: ";
  66. cin >> price;
  67. tempItem.storeInfo(partNum, description, qty, price);
  68. return tempItem;
  69. }
  70. void showValues(const InventoryItem &item)
  71. {
  72. cout << fixed << showpoint << setprecision(2) << endl;
  73. cout << "Part Number : " << item.getPartNum() << endl;
  74. cout << "Description : " << item.getDescription () << endl;
  75. cout << "Units On Hand: " << item.getOnHand () << endl;
  76. cout << "Price : $"<< item.getPrice () << endl;
  77. }
程序输出结果:

Enter data for the new part number
Part number: 175
Description:Hammer
Quantity on hand: 12
Unit price: 7.49

Part Number : 175
Description : Hammer
Units On Hand: 12
Price : $7.49