uacs2024

导航

2024年3月1日 #

C++ 把引用作为返回值

摘要: 通过使用引用来替代指针,会使 C++ 程序更容易阅读和维护。C++ 函数可以返回一个引用,方式与返回一个指针类似。 当函数返回一个引用时,则返回一个指向返回值的隐式指针。这样,函数就可以放在赋值语句的左边。例如,请看下面这个简单的程序: 1 #include <iostream> 2 3 using 阅读全文

posted @ 2024-03-01 21:38 ᶜʸᵃⁿ 阅读(39) 评论(0) 推荐(0) 编辑

C++ 把引用作为参数

摘要: 1 #include <iostream> 2 using namespace std; 3 4 // 函数声明 5 void swap(int& x, int& y); 6 7 int main () 8 { 9 // 局部变量声明 10 int a = 100; 11 int b = 200; 阅读全文

posted @ 2024-03-01 20:59 ᶜʸᵃⁿ 阅读(6) 评论(0) 推荐(0) 编辑

C++ 从函数返回指针

摘要: C++ 允许从函数返回指针,必须声明一个返回指针的函数: int * myFunction() C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static变量。 1 #include <iostream> 2 #include <ctime> 3 #include <cstdlib> 阅读全文

posted @ 2024-03-01 20:23 ᶜʸᵃⁿ 阅读(13) 评论(0) 推荐(0) 编辑

C++ 指针 vs 数组

摘要: 指针和数组并不是完全互换的 1 #include <iostream> 2 3 using namespace std; 4 const int MAX = 3; 5 6 int main () 7 { 8 int var[MAX] = {10, 100, 200}; 9 10 for (int i 阅读全文

posted @ 2024-03-01 19:43 ᶜʸᵃⁿ 阅读(3) 评论(0) 推荐(0) 编辑

C++static 存储类

摘要: 1 #include <iostream> 2 3 // 函数声明 4 void func(void); 5 6 int main() 7 { 8 int count=10; 9 while(count--) 10 { 11 func(); 12 std::cout << ",变量 count 为 阅读全文

posted @ 2024-03-01 14:50 ᶜʸᵃⁿ 阅读(7) 评论(0) 推荐(0) 编辑