ADVPT-C++复习准备

Abstract

复习Advpt考试用的,主要是STL库和C++基本语法

指针和引用

三种const 指针

  1. 无const: int *p = &something,可以直接通过更改*p的右值来更改p的值,即给p赋值也会更改something的值

  2. const 在前: a pointer to **const something ** const int *p = &somethingint const *p = &something 等价,不可以直接通过更改*p的右值来更改*p的值,但是可以更改something的值从而更改*p的值。可以通过再次赋值来更改*p指向的地址: point = &other;

  3. const 在后:is a constant pointer to something int * const p = &something,可以直接通过更改*p的右值来更改*p的值 即, *point = 右值;
    ,但是可以更改something的值从而更改*p的值。不可以通过再次赋值来更改*p指向的地址,从一(地址)而终。

  4. const都有: const int const *p = &something ,结合上面两种情况,只能通过更改原地址上的变量给指针修改值,并且这个指针不允许指向其他地址
    小验证:

#include<iostream>
using namespace std;
int main() {
	int c=8;
	const int* b = &c;
	 int const* a=&c;
	 int *const e = &c;
	*a = 20;//错
	*b = 20;//错
	*e = 20;
	cout << c << endl;
}

this指针

在 C++ 中,每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数。因此,在成员函数内部,它可以用来指向调用对象。也就是自己本身。友元无this指针。
return *this;// 非静态成员函数中使用返回对象本身或者克隆。常见的一种用法。
return this 的话是返回本对象地址
看了stackflow后暂时认为this只会返回对象而不会是地址。有三种不同的方式,分别是以指针,引用和本身的拷贝的方式返回

class myclass {
public:
   // Return by pointer needs const and non-const versions
         myclass* ReturnPointerToCurrentObject()       { return this; }
   const myclass* ReturnPointerToCurrentObject() const { return this; }

   // Return by reference needs const and non-const versions
         myclass& ReturnReferenceToCurrentObject()       { return *this; }
   const myclass& ReturnReferenceToCurrentObject() const { return *this; }

   // Return by value only needs one version.
   myclass ReturnCopyOfCurrentObject() const { return *this; }
};

看起来的确是对的,自己做个了测试

#include<iostream>
using namespace std;

class Box {
public:
	Box() { ; }
	~Box() { ; }
	Box get_address()   //得到this对象的克隆
	{
		return *this;
	}
	Box& get_addressr()   //得到this对象通过reference,因为函数返回值是个引用
	{
		return *this;
	}

	Box* get_addressp()   //得到this对象通过指针,因为函数返回值是个指针
	{
		return this;
	}
	Box get_address2()   //错误 无法从“Box *”转换为“Box”

	{
		return this;
	}
};

int main() {

	Box box1;
	
	Box* ppp = &box1;
	Box p = box1.get_address();
	Box &pr = box1.get_addressr();//可以去掉&,但是加上的意思是pr的地址和box1相同 ,所以下面输出也会是相同
	Box *pp = box1.get_addressp();//不可以去掉*,因为返回的是个指针,这句意思是pp指向了box1(的地址)

	cout << &box1 << endl;//box地址
	cout << &p << endl;//不相同,因为是box克隆的地址
	cout << &pr << endl;//相同
	cout << pp << endl;//相同
	cout << ppp << endl;//相同,ppp指向的就是box1的地址

	return 0;
}

STL的智能指针

在memory里定义
1.unique_ptr 这个指针一旦指向了某个对象,那其他的指针不可再指它这个对象了。即 unique_ptr 不共享它所管理的对象。unique_ptr objects own their pointer uniquely

2.shared_ptr 这个指针就大方了。 Objects of shared_ptr types have the ability of taking ownership of a pointer and share that ownership

3.auto_ptr,没unique安全

4.weak_ptr 辅助shared的

引用reference &

benefits of reference in c++

1.A reference variable does not consume any extra memory. It has the same memory address as the variable it refers to. While a pointer needs extra space for itself.

2.In order to access the value of the referenced variable using pointer, we need to use a dereferencing operator(*) whereas we can do the same using reference variable without any dereferencing method.
总结:省内存,还有个优点是在使用的时候很容易做chain式拓展,但是估计不会考,知道就行

常识

问:为啥operator和一些函数前面整了个&?
答:首先说明返回值是个引用。有俩好处:
①允许进行连续赋值
②防止返回对象(返回对象也可以进行连续赋值)的时候调用拷贝构造函数和析构函数导致不必要的开销,降低赋值运算符等的效率

class TestClass {
    private:
        int number;
    public:
        TestClass& operator+=(const TestClass& rhs) {
            number += rhs.number;
            return *this;
        }
};

作者:钟宇腾
链接:https://www.zhihu.com/question/22821783/answer/22759545
来源:知乎

上例是一个C++的不完整的简单类,其中+=运算符,它本身的意义是「自增,并返回自增后的值」,所以就要返回自己,而不是返回一个自己的拷贝。结合上一节关于this,所以就应该返回自己。

STL库和语法

考过的一些库的作用

1.One object from the stream library: cin cout cerr 之类他们包含在iostream里,iostreamisstream里声明

  1. List three different parts of the standard library by specifying the corresponding header file:从c++网站上看,stl库分五部分,分别是
种类代表头文件
C Libarycmath,cstring(无类,只是一堆function)
Input/outputiostream(内部没有同名类,可用istream答题,他有)
Containerslist(list)
Multi-threadingatomic(同名类) , thread
Otheralgorithm(无),chrono(duration都是和时间相关的)

3.显式转换的一些keywords:reinterpret_cast, const_cast(去const 用的), static_cast, dynamic_cast,如果是智能指针的转换就加个point_在中间
4. C++11和14,17,20部分新特性

种类代表头文件
C++11auto lambda
C++14/outputgenric lambda expression,variable templates
C++17constexpr if
C++20using enum
背俩就行
  1. std::function和std::bind 的用法: 简单来说就是把函数又封包了一次,感觉有点脱裤子放屁
    例题:

Given is the function fct which takes a pointer to a double and a const reference to a std::list as input and returns no value. Using the std::function library type, define the variable f and initialize it with fct.

答:

 #include <iostream>
 #include<functional>
 #include<list>
using namespace std;
void fct(double*input1,const list<double> &input2){;}
std::function<void(double *a,const list<double> input2) > f=fct;//可用using或者typedef来简化一下

关于bind,他是 auto variable = bind(TestFunc, std::placeholders::_1,……等等参数);

6 CV-qualifier: const and volatile
const - defines that the type is constant.
volatile - defines that the type is volatile.

7.What is a friend function? :A friend function of a class is defined outside that class’ scope but it has the right to access all private and protected members of the class.

8.When should be a “destructor” declared virtual and why?
In some cases ,use Virtual destructors avoiding undefined behaviour/double free? especially when you might potentially delete an instance of a derived class through a pointer to base class

9.Variable declaration(could be many times) tells the compiler about data type and size of the variable. Whereas, variable definition(just once) allocates memory to the variable

10.Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource that must be acquired before use to the lifetime of an object.

11.Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution.

12.内存分配:new and delete,malloc() and free().

13.What is a static member function?

A static member function is a special member function, which is used to access only static data members, any other normal data member cannot be accessed through static member function.

Static members are data members (variables) or methods that belong to a static or a non static class itself, rather than to objects of the class. Static members always remain the same, regardless of where and how they are used.

14.What is a pure virtual function? 等于0,子类必须implement

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值