之前面试被问到C++里static的作用是什么,但我却只知道static在java里的作用是什么,于是就google了一下c++相关面试题,发现这个同学总结的很棒。
就记录一下。
原文地址:C/C++面试知识总结
侵删
--------------------------正文分割线------------------------------------
// 类 class A { private: const int a; // 常对象成员,只能在初始化列表赋值 public: // 构造函数 A() { }; A(int x) : a(x) { }; // 初始化列表 // const可用于对重载函数的区分 int getValue(); // 普通成员函数 int getValue() const; // 常成员函数,不得修改类中的任何数据成员的值 }; void function() { // 对象 A b; // 普通对象,可以调用全部成员函数 const A a; // 常对象,只能调用常成员函数、更新常成员变量 const A *p = &a; // 常指针 const A &q = a; // 常引用 // 指针 char greeting[] = "Hello"; char* p1 = greeting; // 指针变量,指向字符数组变量 const char* p2 = greeting; // 指针变量,指向字符数组常量 char* const p3 = greeting; // 常指针,指向字符数组变量 const char* const p4 = greeting; // 常指针,指向字符数组常量 } // 函数 void function1(const int Var); // 传递过来的参数在函数内不可变 void function2(const char* Var); // 参数指针所指内容为常量 void function3(char* const Var); // 参数指针为常指针 void function4(const int& Var); // 引用参数在函数内为常量 // 函数返回值 const int function5(); // 返回一个常数 const int* function6(); // 返回一个指向常量的指针变量,使用:const int *p = function6(); int* const function7(); // 返回一个指向变量的常指针,使用:int* const p = function7();
volatile int i = 10;
this
指针是一个隐含于每一个成员函数中的特殊指针。它指向正在被该成员函数操作的那个对象。this
指针,然后调用成员函数,每次成员函数存取数据成员时,由隐含使用 this
指针。this
指针被隐含地声明为: ClassName *const this
,这意味着不能给 this
指针赋值;在 ClassName
类的 const
成员函数中,this
指针的类型为:const ClassName* const
,这说明不能对 this
指针所指向的这种对象是不可修改的(即不能对这种对象的数据成员进行赋值操作);this
并不是一个常规变量,而是个右值,所以不能取得 this
的地址(不能 &this
)。this
指针:list
。// 声明1(加 inline,建议使用) inline int functionName(int first, int secend,...); // 声明2(不加 inline) int functionName(int first, int secend,...); // 定义 inline int functionName(int first, int secend,...) {/****/};
优点
缺点
Are "inline virtual" member functions ever actually "inlined"?
inline virtual
唯一可以内联的时候是:编译器知道所调用的对象是哪个类(如 Base::who()
),这只有在编译器具有实际对象而不是对象的指针或引用时才会发生。#include <iostream> using namespace std; class Base { public: inline virtual void who() { cout << "I am Base\n"; } virtual ~Base() {} }; class Derived : public Base { public: inline void who() // 不写inline时隐式内联 { cout << "I am Derived\n"; } }; int main() { // 此处的虚函数 who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 Base b; b.who(); // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 Base *ptr = new Derived(); ptr->who(); // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 delete ptr; ptr = nullptr; system("pause"); return 0; }
断言,是宏,而非函数。assert 宏的原型定义在<assert.h>
(C)、<cassert>
(C++)中,其作用是如果它的条件返回错误,则终止程序执行。
如
assert( p != NULL );
设定结构体、联合以及类成员变量以 n 字节方式对齐
如
#pragma pack(push) // 保存对齐状态 #pragma pack(4) // 设定为 4 字节对齐 struct test { char m1; double m4; int m3; }; #pragma pack(pop) // 恢复对齐状态
extern "C"
修饰的变量和函数是按照 C 语言方式编译和连接的extern "C"
的作用是让 C++ 编译器将 extern "C"
声明的代码当作 C 语言代码处理,可以避免 C++ 因符号修饰导致代码不能和C语言库中的符号进行链接的问题。
#ifdef __cplusplus extern "C" { #endif void *memset(void *, int, size_t); #ifdef __cplusplus } #endif
// c typedef struct Student { int age; } S;
等价于
// c struct Student { int age; }; typedef struct Student S;
此时 S
等价于 struct Student
,但两个标识符名称空间不相同。
另外还可以定义与 struct Student
不冲突的 void Student() {}
。
由于编译器定位符号的规则(搜索规则)改变,导致不同于C语言。
一、如果在类标识符空间定义了 struct Student {...};
,使用 Student me;
时,编译器将搜索全局标识符表,Student
未找到,则在类标识符内搜索。
即表现为可以使用 Student
也可以使用 struct Student
,如下:
// cpp struct Student { int age; }; void f( Student me ); // 正确,"struct" 关键字可省略
二、若定义了与 Student
同名函数之后,则 Student
只代表函数,不代表结构体,如下:
typedef struct Student { int age; } S; void Student() {} // 正确,定义后 "Student" 只代表此函数 //void S() {} // 错误,符号 "S" 已经被定义为一个 "struct Student" 的别名 int main() { Student(); struct Student me; // 或者 "S me"; return 0; }
总的来说,struct 更适合看成是一个数据结构的实现体,class 更适合看成是一个对象的实现体。
explicit 修饰的构造函数可用来防止隐式转换
如下
class Test1 { public: Test1(int n) // 普通构造函数 { num=n; } private: int num; }; class Test2 { public: explicit Test2(int n) // explicit(显式)构造函数 { num=n; } private: int num; }; int main() { Test1 t1=12; // 隐式调用其构造函数,成功 Test2 t2=12; // 编译错误,不能隐式调用其构造函数 Test2 t2(12); // 显式调用成功 return 0; }
using namespace_name::name
using namespace std;
污染命名空间一般说来,使用 using 命令比使用 using 编译命令更安全,这是由于它只导入了制定的名称。如果该名称与局部名称发生冲突,编译器将发出指示。using编译命令导入所有的名称,包括可能并不需要的名称。如果与局部名称发生冲突,则局部名称将覆盖名称空间版本,而编译器并不会发出警告。另外,名称空间的开放性意味着名称空间的名称可能分散在多个地方,这使得难以准确知道添加了哪些名称。
尽量不要使用
using namespace std;
应该使用
int x; std::cin >> x ; std::cout << x << std::endl;
或者
using std::cin; using std::cout; using std::endl; int x; cin >> x; cout << x << endl;
::
可以加在类型名称(类、类成员、成员函数、变量等)前,表示作用域为全局命名空间
如
int count = 0; // global count int main() { int count = 0; // local count ::count = 1; // set global count to 1 count = 2; // set local count to 2 return 0; }
好处
面向对象程序设计(Object-oriented programming,OOP)是种具有对象概念的程序编程典范,同时也是一种程序开发的抽象方针。
面向对象三大特征 —— 封装、继承、多态
关键字 | 当前类 | 包内 | 子孙类 | 包外 |
---|---|---|---|---|
public | √ | √ | √ | √ |
protected | √ | √ | √ | × |
friendly | √ | √ | × | × |
private | √ | × | × | × |
函数重载
class A { public: void do(int a); void do(int a, int b); };
注意:
class Shape // 形状类 { public: virtual double calcArea() { ... } virtual ~Shape(); }; class Circle : public Shape // 圆形类 { public: virtual double calcArea(); ... }; class Rect : public Shape // 矩形类 { public: virtual double calcArea(); ... }; int main() { Shape * shape1 = new Circle(4.0); Shape * shape2 = new Rect(5.0, 6.0); shape1->calcArea(); // 调用圆形类里面的方法 shape2->calcArea(); // 调用矩形类里面的方法 delete shape1; shape1 = nullptr; delete shape2; shape2 = nullptr; return 0; }
class Shape { public: Shape(); // 构造函数不能是虚函数 virtual double calcArea(); virtual ~Shape(); // 虚析构函数 }; class Circle : public Shape // 圆形类 { public: virtual double calcArea(); ... }; int main() { Shape * shape1 = new Circle(4.0); shape1->calcArea(); delete shape1; // 因为Shape有虚析构函数,所以delete释放内存时,先调用子类析构函数,再调用基类析构函数,防止内存泄漏。 shape1 = NULL; return 0; }
// 定义 struct Date { int ival; string s; } // 初始化 Data vall = { 0, "Anna" };
.rodata section
,见:目标文件存储结构),存放虚函数指针,如果派生类实现了基类的某个虚函数,则在虚表中覆盖原本基类的那个虚函数指针,在编译时根据类的声明创建。虚继承用于解决多继承条件下的菱形继承问题(浪费存储空间、存在二义性)。
底层实现原理与编译器相关,一般通过虚基类指针和虚基类表实现,每个虚继承的子类都有一个虚基类指针(占用一个指针的存储空间,4字节)和虚基类表(不占用类对象的存储空间)(需要强调的是,虚基类依旧会在子类里面存在拷贝,只是仅仅最多存在一份而已,并不是不在子类里面了);当虚继承的子类被当做父类继承时,虚基类指针也会被继承。
实际上,vbptr 指的是虚基类表指针(virtual base table pointer),该指针指向了一个虚基类表(virtual table),虚表中记录了虚基类与本类的偏移地址;通过偏移地址,这样就找到了虚基类成员,而虚继承也不用像普通多继承那样维持着公共基类(虚基类)的两份同样的拷贝,节省了存储空间。
申请内存,确认是否申请成功
char *str = (char*) malloc(100); assert(str != nullptr);
释放内存后指针置空
free(p); p = nullptr;
int main() { T* t = new T(); // 先内存分配 ,再构造函数 delete t; // 先析构函数,再内存释放 return 0; }
Is it legal (and moral) for a member function to say delete this?
合法,但:
new
(不是 new[]
、不是 placement new、不是栈上、不是全局、不是其他对象成员)分配的delete this
的成员函数是最后一个调用 this 的成员函数delete this
后面没有调用 this 了delete this
后没有人使用了原文:https://www.cnblogs.com/yaoyudadudu/p/9186866.html