程序作用是模拟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技术内幕 上面的

解决方案 »

  1.   

    一定是印反了。应该是:
    nPos = pMot->GetPosition();
      

  2.   

    pMot->GetPosition() = nPos;            <<=====对函数调用也能够赋值?
    注意了吗?这个函数返回的是引用,这个语法没错。对引用可以赋值。
    对引用的赋值常用在对“=”号的重载中。
      

  3.   

    为什么不可以?看#include <stdio.h>int data=100;
    int& getaddr()
    {
     return data;
    }
    main()
    {
     int a=200;
     getaddr()=a;
     printf("data=%d a=%d",data,a);
     return 0;
    }结果 
    data=200 a=200