程序作用是模拟COM的实现struct IMotion
{
virtual void Fly() = 0;
virtual int& GetPosition() = 0;
};class CSpaceship : public IMotion
{
protected:
int m_nPosition;
public:
CSpaceship() { m_nPosition = 0; }
void Fly();
int& GetPosition() { return m_nPosition; }
};
The actual code for the spaceship-related functions—including GetClassObject—is located in the component part of the program. The client part calls the GetClassObject function to construct the spaceship and to obtain an IMotion pointer. Both parts have access to the IMotion declaration at compile time. Here's how the client calls GetClassObject:
IMotion* pMot;
GetClassObject(CLSID_CSpaceship, IID_IMotion, (void**) &pMot);
Assume for the moment that COM can use the unique integer identifiers CLSID_CSpaceship and IID_IMotion to construct a spaceship object instead of some other kind of object. If the call is successful, pMot points to a CSpaceship object that GetClassObject somehow constructs. As you can see, the CSpaceship class implements the Fly and GetPosition functions, and our main program can call them for the one particular spaceship object, as shown here:
int nPos = 50;
pMot->GetPosition() = nPos; <<=====对函数调用也能够赋值?
pMot->Fly();
nPos = pMot->GetPosition();
TRACE("new position = %d\n", nPos);VC6.0技术内幕 上面的
{
virtual void Fly() = 0;
virtual int& GetPosition() = 0;
};class CSpaceship : public IMotion
{
protected:
int m_nPosition;
public:
CSpaceship() { m_nPosition = 0; }
void Fly();
int& GetPosition() { return m_nPosition; }
};
The actual code for the spaceship-related functions—including GetClassObject—is located in the component part of the program. The client part calls the GetClassObject function to construct the spaceship and to obtain an IMotion pointer. Both parts have access to the IMotion declaration at compile time. Here's how the client calls GetClassObject:
IMotion* pMot;
GetClassObject(CLSID_CSpaceship, IID_IMotion, (void**) &pMot);
Assume for the moment that COM can use the unique integer identifiers CLSID_CSpaceship and IID_IMotion to construct a spaceship object instead of some other kind of object. If the call is successful, pMot points to a CSpaceship object that GetClassObject somehow constructs. As you can see, the CSpaceship class implements the Fly and GetPosition functions, and our main program can call them for the one particular spaceship object, as shown here:
int nPos = 50;
pMot->GetPosition() = nPos; <<=====对函数调用也能够赋值?
pMot->Fly();
nPos = pMot->GetPosition();
TRACE("new position = %d\n", nPos);VC6.0技术内幕 上面的
解决方案 »
- 怎么才能让我输入数据的时候让Combo的下拉框自动弹出来
- 动态库中创建的对话框资源怎么释放啊?请问高手
- 想写个三维立体会旋转的正方体!请给个思路!谢谢!
- 投递 WSARecv 时,返回错误,WSAGetLastError() 得到6,是 INVALID SOCKET HANDLE!?
- 多次画图中 弹出 无法获得所需资源 错误 怎么解决
- 如何确定接收缓冲区的大小?有啥原则?
- 请问有谁研究过智能agent或移动agent,大家交流一下?
- 考个高程,对找工作有多大帮助 ???
- 一个价值10分的问题
- 用Modem拨号上网需要TCP/IP协议么?
- 学习一下安装程序的制作,顺便给参与者散点分。
- 不知各位有无遇到过VC70中开发的程序在XP OR WIN 2000 上运行良好,但到了ME,98上就有各种问题,如自画控件会无故增大.不知各位怎样解决的?
nPos = pMot->GetPosition();
注意了吗?这个函数返回的是引用,这个语法没错。对引用可以赋值。
对引用的赋值常用在对“=”号的重载中。
int& getaddr()
{
return data;
}
main()
{
int a=200;
getaddr()=a;
printf("data=%d a=%d",data,a);
return 0;
}结果
data=200 a=200