forked from youngyangyang04/TechCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
10 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
|
||
纯虚函数是在基类中声明的虚函数,它在基类中没有定义,但要求任何派生类都要定义自己的实现方法。在基类中实现纯虚函数的方法是在函数原型后加“=0” | ||
|
||
virtual void funtion1()=0 | ||
`virtual void funtion1()=0` | ||
|
||
原因: | ||
1、为了方便使用多态特性,我们常常需要在基类中定义虚拟函数。 | ||
2、在很多情况下,基类本身生成对象是不合情理的。例如,动物作为一个基类可以派生出老虎、孔雀等子类,但动物本身生成对象明显不合常理。 | ||
|
||
为了解决上述问题,引入了纯虚函数的概念,将函数定义为纯虚函数(方法:virtual ReturnType Function()= 0;),则编译器要求在派生类中必须予以重写以实现多态性。同时含有纯虚拟函数的类称为抽象类,它不能生成对象。这样就很好地解决了上述两个问题。声明了纯虚函数的类是一个抽象类。所以,用户不能创建类的实例,只能创建它的派生类的实例。 | ||
|
||
定义纯虚函数的目的在于,使派生类仅仅只是继承函数的接口。 | ||
**定义纯虚函数的目的在于,使派生类仅仅只是继承函数的接口。** | ||
纯虚函数的意义,让所有的类对象(主要是派生类对象)都可以执行纯虚函数的动作,但类无法为纯虚函数提供一个合理的缺省实现。所以类纯虚函数的声明就是在告诉子类的设计者,“你必须提供一个纯虚函数的实现,但我不知道你会怎样实现它”。 | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
1. 服务对象不同:迭代器主要面对STL容器提供服务,为STL容器提供遍历,删除等功能,而指针主要面向内存底层进行服务。 | ||
2. 安全性:迭代器可以提供更高级的安全性,因为它们可以对越界和空指针进行检查,并且可以被设计为只读或者只写。而普通指针则需要程序员自行管理边界和空指针的情况。 | ||
3. 可移植性:迭代器在不同容器和数据结构之间具有更好的可移植性,因为它们隐藏了底层的实现细节,使得代码更易于重用。普通指针则依赖于具体的数据结构和内存布局。 | ||
4. 功能扩展:标准库定义了多种类型的迭代器,这些迭代器具有不同的特性和功能,如输入迭代器、输出迭代器、前向迭代器、双向迭代器和随机访问迭代器等。这些迭代器提供了更丰富的功能和算法支持。普通指针则只能进行基本的指针运算。 | ||
|
||
总的来说,迭代器提供了更丰富的抽象和功能,使得它们在许多场景下比普通指针更加灵活和安全。 |