64,647
社区成员
发帖
与我相关
我的任务
分享
void Example::use_ptr() const
{
cout<<"3333333333333333333333333333333333333333335555555555555555555555555555555555555555555555555\n";
Example yard(3);
yard.show_ft();
yard.show_in();
int Example::*pt; //声明一个指向类成员的指针,它以偏移量的方式存在
pt = &Example::inches; //将成员inches的偏移量赋值给pt
cout<<"Set pt to &Example::inches:\n";
cout<<"this->pt:"<<this->*pt<<endl; //this是指针,*pt为偏移量,this->*pt相当于this->inches
cout<<"yard.*pt:"<<yard.*pt<<endl; //yard是对象,*pt为偏移量,yard.*pt相当于yard.inches
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)();
}
void Example::use_ptr() const
{
cout<<"3333333333333333333333333333333333333333335555555555555555555555555555555555555555555555555\n";
Example yard(3);
yard.show_ft();
yard.show_in();
int Example::*pt; //声明一个指向类成员的指针,它以偏移量的方式存在
pt = &Example::inches; //将成员inches的偏移量赋值给pt
cout<<"Set pt to &Example::inches:\n";
cout<<"this->pt:"<<this->*pt<<endl; //this是指针,*pt为偏移量,this->*pt相当于this->inches
cout<<"yard.*pt:"<<yard.*pt<<endl; //yard是对象,*pt为偏移量,yard->*pt相当于yard.inches
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)();
}
#include <iostream>
using namespace std;
class MyClass
{
public:
int m_Id;
};
int main(void)
{
int MyClass::*pId;
MyClass c;
//指向成员的指针
pId = &MyClass::m_Id;
//偏移方式获取m_Id的地址并赋值
c.*pId = 5;
cout << c.m_Id ;
return 0;
}
43981
52651
car.use_ptr() output:
3 feet
36 inches
Set pt to &Example::inches:
this->pt:180
yard.*pt:36
Set pt to &Example::feet:
this->pt:15
yard.*pt:3
Set pf to &Example::show_in:
Using(this->pf):180 inches
Using(yard.*pf)():36 inches
van.use_ptr() output:
3 feet
36 inches
Set pt to &Example::inches:
this->pt:240
yard.*pt:36
Set pt to &Example::feet:
this->pt:20
yard.*pt:3
Set pf to &Example::show_in:
Using(this->pf):240 inches
Using(yard.*pf)():36 inches
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
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;
};
#endif /*EXAMPLE_H_*/
#include <iostream.h>
#include "Example.h"
Example::Example()
{
feet = 0;
inches = 0;
}
Example::Example(int ft)
{
feet = ft;
inches = 12*ft;
}
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
{
cout<<"3333333333333333333333333333333333333333335555555555555555555555555555555555555555555555555\n";
Example yard(3);
yard.show_ft();
yard.show_in();
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)();
}
#include <iostream>
#include "Example.h"
using namespace std;
int main() {
Example car(15);
Example van(20);
Example garage;
cout<<0xabcd<<endl;
cout<<0xcdab<<endl;
cout<<"car.use_ptr() output:\n";
car.use_ptr();
cout<<"\nvan.use_ptr() output:\n";
van.use_ptr();
return 0;
}
class some
{
int a;
int b;
some()
{
a=0;
b=0;
}
}
class some;
some *p;