如何声明和使用指向类数据成员的指针?

liufangbj 2008-06-10 02:27:48
如题
谢谢啦~~
...全文
160 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
liufangbj 2008-06-10
  • 打赏
  • 举报
回复
谢谢楼上的
^^
yuzl32 2008-06-10
  • 打赏
  • 举报
回复

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)();
}


yuzl32 2008-06-10
  • 打赏
  • 举报
回复

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)();
}
axing0308008341 2008-06-10
  • 打赏
  • 举报
回复
你得先有指向类对象的指针,才能指得到他的数据成员吧~
yuzl32 2008-06-10
  • 打赏
  • 举报
回复

#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;
}

/out:Client.exe
Client.obj

E:\>Client
5
liufangbj 2008-06-10
  • 打赏
  • 举报
回复
输出结果:
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
liufangbj 2008-06-10
  • 打赏
  • 举报
回复
我不明白如下程序的输出:

Exapmle.h
#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_*/

Exapmle.cpp

#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;
}
SenerityChzr 2008-06-10
  • 打赏
  • 举报
回复
class some {
public:
some() { a=0; b=0; }

int a; int b;
}

int some::*piA = NULL; // 声明some的类的成员变量指针,可以指向其任何一个int型public成员变量,


但这样做是不符合C++面向对象理念的……
taodm 2008-06-10
  • 打赏
  • 举报
回复
你就不能找本好点的C++教材翻一翻么?
这是一个基本语法呀。
simo110 2008-06-10
  • 打赏
  • 举报
回复
成员函数就要用函数指针
函数指针还有些特别
simo110 2008-06-10
  • 打赏
  • 举报
回复
using some::a;
int *pa = a;
liufangbj 2008-06-10
  • 打赏
  • 举报
回复
没有人回,自己先顶一下,大家多帮忙啊!!!
liufangbj 2008-06-10
  • 打赏
  • 举报
回复
不是指向类,而是指向类的数据成员. 如下类,声明指针指向a或者b 并使用该指针

class some
{
int a;
int b;
some()
{
a=0;
b=0;
}
}

clhposs 2008-06-10
  • 打赏
  • 举报
回复
class some;

some *p;
在Windows 10或Windows 11操作系统中,用户经常会遇到共享打印机时出现的一系列错误代码,这些错误代码可能会阻碍打印机共享功能的正常使用。常见的错误代码包括0x00000057、0x00000709和0x0000011b,这些代码通常指出了不同的问题,比如权限不足、服务未运行或配置错误等。除此之外,还有一些故障提示如“连接失败”或“内存不足”,这些都可能影响到打印机共享的稳定性。 要解决这些故障,首先要确保打印机已经正确地连接到网络,并且在需要共享的电脑上进行了设置。确保打印机驱动程序是最新的,并且在共享设置中没有错误配置。对于权限问题,需要检查网络上的用户账户是否具有足够的权限来访问共享打印机。同时,也要确保打印机服务正在运行,特别是“Print Spooler”服务,因为这是打印机共享服务的核心组件。 在某些情况下,问题可能与操作系统的更新有关,如升级到最新版的Windows 10或Windows 11后可能出现的兼容性问题。这时,可能需要查看微软的官方支持文档来获取特定的解决方案或更新。 对于错误代码0x00000057,这通常是由于没有足够的权限来访问网络打印机或其共享资源,解决方法是确保网络打印机的权限设置正确,包括在组策略中设置相应的访问权限。而0x00000709错误可能是由于打印机驱动问题或打印机端口配置错误,可以尝试重新安装或更新打印机驱动来解决。至于0x0000011b错误,这往往是因为打印机队列服务的问题,检查并重启“Print Spooler”服务通常是解决这问题的常见手段。 至于“连接失败”或“内存不足”这故障,通常与客户端和打印机之间的网络连接以及打印机本地资源的使用情况有关。检查网络连接,确保打印机所在的网络段没有故障或中断。同时,如果打印机的打印队列长时间得不到处理,可能会导致内存不足的情况,这时可能需要清理打印队列或增加打印机的内存配置。 为了帮助用户更快速地解决这些问题,市面上出现了各种打印机共享错误修复工具。这些工具往往通过预设的修复程序来自动检测和修正打印机共享中常见的问题。它们可以快速检查打印机驱动、网络连接以及共享设置,并且能够提供一键修复功能,大幅减少了用户自行排查和解决问题的难度。 然而,在使用这些修复工具之前,用户应确保这些工具的来源是安全可靠的,避免因使用不当的修复工具而引发其他系统安全或隐私问题。用户可以到官方平台或者信誉良好的软件提供商处下载这些工具。通过细心检查打印机的共享设置,及时更新驱动程序和服务,以及合理使用修复工具,大多数共享打印机的问题都可以得到有效的解决。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧