memb_pt.cpp
来源: 开始学习 C++
点击上方按钮打开代码编辑器和可视化视图
⚠️ 交互式运行提示
为了防止网页挂起,所有 cin 输入语句已被自动注释。请直接在代码编辑器中修改变量的值来进行测试。
书本配套例题
原始代码预览
// memb_pt.cpp -- dereferencing pointers to class members
#include <iostream>
using namespace std;
class Example
{
private:
int feet;
int inches;
public:
Example();
Example(int ft);
~Example();
void show_in() const;
void show_ft() const;
void use_ptr() const;
};
Example::Example()
{
feet = 0;
inches = 0;
}
Example::Example(int ft)
{
feet = ft;
inches = 12 * feet;
}
Example::~Example()
{
}
void Example::show_in() const
{
cout << inches << " inches\n";
}
void Example::show_ft() const
{
cout << feet << " feet\n";
}
void Example::use_ptr() const
{
Example yard(3);
int Example::*pt;
pt = &Example::inches;
cout << "Set pt to &Example::inches:\n";
cout << "this->pt: " << this->*pt << endl;
cout << "yard.*pt: " << yard.*pt << endl;
pt = &Example::feet;
cout << "Set pt to &Example::feet:\n";
cout << "this->pt: " << this->*pt << endl;
cout << "yard.*pt: " << yard.*pt << endl;
void (Example::*pf)() const;
pf = &Example::show_in;
cout << "Set pf to &Example::show_in:\n";
cout << "Using (this->*pf)(): ";
(this->*pf)();
cout << "Using (yard.*pf)(): ";
(yard.*pf)();
}
int main()
{
Example car(15);
Example van(20);
Example garage;
cout << "car.use_ptr() output:\n";
car.use_ptr();
cout << "\nvan.use_ptr() output:\n";
van.use_ptr();
cin.get();
return 0;
}
运行结果 / 调试信息
等待编译...
Graph loading...
0 / 0