C++基础:引用,内联函数,auto,类的两种定义方式
- 创业
- 2025-09-02 09:12:01

#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> using namespace std; int main() { //权限缩小和放大的规则:适用于引用和指针 //权限放大 不能把const给非const,const只能给const const int a = 10; //int& b = a;//不行,权限放大 const int& b = a;
//权限缩小 非const既可以给非const,也可以给const int c = 10; int& d = c; const int& e = c;
const int* cp1 = &a; //int* p1 = cp1;//不能,属于权限的放大
int* cp2 = &c; const int* p2 = cp2;//可以,属于权限的缩小
//下面可以不跟上面的规则 const int x = 10; int y = x;
int z = 20; const int r = z; return 0; }
&在类型前面是引用,在变量前面是取地址 1.引用做参数 void swap_c(int* p1, int* p2) { int tmp = *p1; *p1 = *p2; *p2 = tmp; }
void swap_cpp(int& r1, int& r2)//引用 { int tmp = r1; r1 = r2; r2 = tmp; }
int main() { int a = 1; int b = 0; swap_c(&a, &b); swap_cpp(a, b); return 0; }
2.引用做返回值 int Count1() { static int n = 0; n++; return n; }
int& Count2() { static int n = 0; n++; return n; }
int main() { const int& r1 = Count1(); int& r2 = Count2(); return 0; }
int& Add(int a, int b) { static int c = a + b;//只会被执行一次 return c; }
int& Add(int a, int b) { static int c = 0; c = a + b;//这个时候会变成7 return c; }
int main() { int& ret = Add(1, 2); Add(3, 4); cout << "Add(1, 2) is :" << ret << endl; return 0; }
#include <time.h> struct A { int a[10000]; }; void TestFunc1(A a) {}
void TestFunc2(A& a) {} int main() { A a; // 以值作为函数参数 size_t begin1 = clock(); for (size_t i = 0; i < 10000; ++i) TestFunc1(a); size_t end1 = clock(); // 以引用作为函数参数 size_t begin2 = clock(); for (size_t i = 0; i < 10000; ++i) TestFunc2(a); size_t end2 = clock(); // 分别计算两个函数运行结束后的时间 cout << "TestFunc1(A)-time:" << end1 - begin1 << endl; cout << "TestFunc2(A&)-time:" << end2 - begin2 << endl; return 0; }
引用和指针的区别 int main() { int a = 10; int& b = a; int c = 10; b = c;
int* p = &a; p = &c;
char c1 = 0; char& c2 = c1;
char* p1 = &c1; return 0; }
int Add(int left, int right) { return left + right; }
inline void Swap(int& x, int& y) { int tmp = x; x = y; y = tmp; }
//频繁调用Swap,调用函数需要栈帧,是有消耗的 //如何解决:1.C语言使用宏函数,2.C++使用内联函数 int main() { int ret = Add(1, 2); int a = 1; int b = 2; Swap(a, b); return 0; }
int main() { int a = 0; auto b = a;//b的类型是根a的类型自动确定的 int& c = a; auto& d = a; auto* p1 = &a; auto p2 = &a;
cout << typeid(a).name() << endl; cout << typeid(b).name() << endl; cout << typeid(c).name() << endl; cout << typeid(d).name() << endl; cout << typeid(p1).name() << endl; cout << typeid(p2).name() << endl;
return 0; }
int main() { int array[] = { 1,2,3,4,5 }; //C语言 for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i) { array[i] *= 2; }
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i) { cout << array[i] << " "; } cout << endl;
//C++ for (auto& e : array)//因为array已经不是数组了 { e *= 2; }
for (auto e : array) { cout << e << " "; } cout << endl;
return 0; }
void f(int) { cout << "整形" << endl; } void f(int*) { cout << "整型指针" << endl; } int main() { //C int* p1 = NULL;
//C++ 推荐下面这种写法 int* p2 = nullptr; f(0); f(NULL);//预处理后f(0) f(nullptr);//f(void*()0) return 0; }
class的默认访问权限为private,struct为public class Student { void ShowInfo() { cout << _name << endl; cout << _age << endl; cout << _stuid << endl; } public: int GetAge() { return _age; } //private: char _name; int _age; int _stuid; };
int main() { Student s1; Student s2; s1._name = "peter"; s1._age = 18; s1._stuid = 1; s1.ShowInfo; return 0; }
C++基础:引用,内联函数,auto,类的两种定义方式由讯客互联创业栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“C++基础:引用,内联函数,auto,类的两种定义方式”