
本文共 4257 字,大约阅读时间需要 14 分钟。
this ���������������������������������������������������
1. this ������
��� C++ ��������������������������������������� this
���������������������������������������������������������������������������������������������������������������������������������������������������������
��������������������������������������������������������� this
������������������������������������������������������������������������������������������������������������������ this
������������������������������������������������������
���������������������������Talk("hello world")
��������������������������������� Talk(this, "hello world")
���
class Human { private: void Talk(string msg) { cout << msg << endl; } public: void IntroduceSelf() { Talk("hello world"); // ��������� Talk(this, "hello world") }};
2. ������������������
������ C++ ���������������������������������������������������������������������������
- ������
->
������������������������������������ - ������������������������������������������������������������������������������������������
���������
class Box { public: Box(double l = 2.0, double b = 2.0, double h = 2.0) { length = l; breadth = b; height = h; } double Volume() { return length * breadth * height; }};int main() { Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); Box* ptrBox; ptrBox = &Box1; cout << "Volume of Box1: " << ptrBox->Volume() << endl; ptrBox = &Box2; cout << "Volume of Box2: " << ptrBox->Volume() << endl; return 0;}
3. ������������
���������������������������������������������������������������������������������������������������������������
3.1 ������������������
������������������������������������������������������������������������������������������������
- ���������������������������������������������������������������
- ���������������������������������������������������������������
- ���������������������������������������������������������������
$::
��������������������� - ������������������������������������������������������
- ������������������������������������������������������
���������
class Box { public: Box(double l = 2.0, double b = 2.0, double h = 2.0) { length = l; breadth = b; height = h; objectCount++; } static int objectCount; double Volume() { return length * breadth * height; }};int Box::objectCount = 0; // ���������������������������
3.2 ������������������
��������������������������������� static
���������������������������������������������������������������������������������������
- ������������������������������������������������������������������������������������
- ������������������������������������������������������������������������������������������������������
$::
���������
���������
class Box { public: Box(double l = 2.0, double b = 2.0, double h = 2.0) { length = l; breadth = b; height = h; objectCount++; } static int objectCount; static int getCount() { return objectCount; } double Volume() { return length * breadth * height; }};int main() { // ��������������������������������������� cout << "������������: " << Box::getCount() << endl; Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); // ��������������������������������� cout << "������������: " << Box::getCount() << endl; return 0;}
���������������������������������
������������: 0
������������������
������������: 2
���������������������������������������������������������������������
- ������������������������������������������������������������������������������
- ���������������������������������������������������������������������������������������������������������
- ������������������������������������������������������
������������������������������������������ this
���������������������������������������������������������������������������
发表评论
最新留言
关于作者
