[C++] c++ 通过引用传递对象 →→→→→进入此内容的聊天室

来自 , 2020-02-12, 写在 C++, 查看 111 次.
URL http://www.code666.cn/view/61b1fb3f
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class SimpleCat {
  5. public:
  6.         SimpleCat(); // constructor
  7.         SimpleCat(SimpleCat&); // copy constructor
  8.         ~SimpleCat(); // destructor
  9. };
  10.  
  11. SimpleCat::SimpleCat() {
  12.         cout << "Simple Cat Constructor..." << endl;
  13. }
  14.  
  15. SimpleCat::SimpleCat(SimpleCat&) {
  16.         cout << "Simple Cat Copy Constructor..." << endl;
  17. }
  18.  
  19. SimpleCat::~SimpleCat() {
  20.         cout << "Simple Cat Destructor..." << endl;
  21. }
  22.  
  23. SimpleCat FunctionOne(SimpleCat theCat);
  24. SimpleCat* FunctionTwo(SimpleCat *theCat);
  25.  
  26. int main() {
  27.         cout << "Making a cat..." << endl;
  28.         SimpleCat Frisky;
  29.         cout << "Calling FunctionOne..." << endl;
  30.         FunctionOne(Frisky);
  31.         cout << "Calling FunctionTwo..." << endl;
  32.         FunctionTwo(&Frisky);
  33.         return 0;
  34. }
  35.  
  36. // FunctionOne, passes by value
  37. SimpleCat FunctionOne(SimpleCat theCat) {
  38.         cout << "Function One. Returning... " << endl;
  39.         return theCat;
  40. }
  41.  
  42. // functionTwo, passes by reference
  43. SimpleCat* FunctionTwo(SimpleCat *theCat) {
  44.         cout << "Function Two. Returning... " << endl;
  45.         return theCat;
  46. }
  47.  

回复 "c++ 通过引用传递对象"

这儿你可以回复上面这条便签

captcha