?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <iostream>
#include<string>
using namespace std;
class BookItem
{
private:
string bookName;
size_t cnt;
public:
BookItem(const string&s,size_t c,double p):
bookName(s),cnt(c),price(p)
{}
~BookItem(){}
protected:
double price;
public:
double bookPrice()
{
return this->price;
}
string getBookName()
{
return this->bookName;
}
size_t getBookCount()
{
return this->cnt;
}
virtual double money()
{
return cnt*price;
}
virtual void costMoney()
{
cout<<money()<<endl;
}
};
class BookBatchItem:public BookItem
{
private:
string bookName;
size_t cnt;
public:
BookBatchItem(const string&s,size_t c,double p,double discountRate):
BookItem(s,c,p),cnt(c),discount(discountRate)
{}
~BookBatchItem(){}
private:
double discount;
public:
double money()
{
if(cnt>=10)
return cnt*price*(1.0-discount);
else
return cnt*price;
}
void costMoney()
{
cout<<money()<<endl;
// cout<<cnt<<endl;
// cout<<price<<endl;
// cout<<discount<<endl;
// cout<<"..."<<endl;
}
};
int main()
{
BookItem b1("Uncle Tom's house",11,12.5);
b1.costMoney();
BookBatchItem b2("Gone with wind",11,12.5,0.12);
b2.costMoney();
BookItem* pb=&b1;
pb->costMoney();
pb=&b2;
pb->costMoney();
return 0;
}
|
只有采用“指针->函数()”或“引用.函数()”的方式调用C++类中的虚函数才会执行动态绑定,非虚函数并不具备动态绑定的特征,不管采用任何方式调用都不行。
下面代码中,一个java或者C#的程序员容易犯的一个错误。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
class Base
{
public:
Base() { p = new char ; }
~Base() { delete p; }
private:
char * p ;
};
class Derived:public Base
{
public:
Derived() { d = new char[10] ; }
~Derived() { delete[] d; }
private:
char * d ;
};
int main()
{
Base *pA = new Derived();
delete pA ;
Derived *pA = new Derived();
delete pA ;
}
|
代码中:
执行delete pA时,直接执行~Base析构函数,不会执行~Derived析构函数的,原因在于析构函数不是虚函数。
执行delete pB时,先执行~Derived()然后再执行~Base()。
相比之下,java和C#中,所有的函数调用都是动态绑定的。
关于C++的成员函数调用与绑定方式,可以通过下面的代码测试:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
class Base
{
public:
virtual void Func() { cout<<"Base"<<endl; }
};
class Derived:public Base
{
public:
virtual void Func() { cout<<"Derived"<<endl; }
};
int main()
{
Derived obj;
Base * p1 = &obj;
Base & p2 = obj;
Base obj2 ;
obj.Func() ; //静态绑定,Derived的func
p1->Func(); //动态绑定,Derived的func
(*p1).Func(); //动态绑定,Derived的func
p2.Func(); //动态绑定,Derived的func
obj2.Func(); //静态绑定,Base的func
return 0 ;
}
|
可以看出“对象名.函数()”属于静态绑定,当然,使用指针转换为对象的方式应该属于指针调用那一类了,至于“类名::函数()”毫无疑问属于静态绑定。
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 95
-
2025-05-25 92
-
2025-06-04 46
-
2025-05-29 53
-
2025-05-29 38
热门评论

