使用指向类的指针调用成员函数时获取System.AccessViolationException

时间:2013-01-09 04:28:13

标签: c++ design-patterns inheritance protocols dereference

using namespace std;

class Layer
{
protected:
    Layer *lower;
    Layer *upper;
public:
    Layer(Layer *lo,Layer *up):lower(lo),upper(up)
    {}
    virtual void send()=0;
    virtual void receive()=0;
};
class Physical_Layer:public Layer
{
public:
    Physical_Layer(Layer *p):Layer(NULL,p)
    {
        cout<<"Physical_Layer constructed"<<endl;
    }
    virtual void send()
    {
        cout<<"Data send from Physical_Layer"<<endl;
        receive();
    }
    virtual void receive()
    {
        cout<<"Physical_Layer calling receive of DataLink_Layer"<<endl;
        upper->receive();
    }
};
class DataLink_Layer:public Layer
{
public:
    DataLink_Layer(Layer *p):Layer(new Physical_Layer(this),p)
    {
        cout<<"DataLink_Layer Constructed"<<endl;
        lower->send();
    }
    virtual void send()
    {
        cout<<"Data send from DataLink_Layer"<<endl;
        lower->send();
    }
    virtual void receive()
    {
        cout<<"DataLink_Layer calling receive of Application_Layer"<<endl;
        cout<<typeid(upper).name()<<endl;



        upper->receive();



    }
};
class Application_Layer:public Layer
{
public:
    Application_Layer():Layer(new DataLink_Layer(this),NULL)
    {
        cout<<"Application_Layer Constructed"<<endl;
        send();
    }
    virtual void send()
    {
        cout<<"Sending data from Application_Layer"<<endl;
        lower->send();
    }
    virtual void receive()
    {
        cout<<"Receiving data at Application_Layer"<<endl;
    }
};

int main()
{
    Layer *l=new Application_Layer();
}

我试图使用协议设计模式模拟三层协议栈。但是,当解除引用DataLink_Layer接收中的upper-gt;接收时,我得到一个运行时异常:System.AccessViolationException。我为什么得到它?

1 个答案:

答案 0 :(得分:3)

DataLink_Layer的构造函数尝试在Application_Layer基类Layer*构建之前通过Layer回调Application_Layer(您目前仍在评估new DataLink_Layer(this)

只需在upper->receive()构造函数中调用DataLink_Layer即可更清楚地看到这一点。

这个FAQ解释了在构造函数中使用this的更多信息。

这个更简单的例子可以更清楚地说明问题:

struct C;
struct A
{
    A(C* c) {};
    virtual void Foo() = 0;
};

struct C
{
    C(A* a)
    {
        a->Foo();
    }
};

struct B : public A
{
    B() : A(new C(this)) {}
    void Foo() {}
};

int main()
{
    B b;
}

通常,不应使用构造函数对部分构造的对象执行复杂的调用堆栈。只需在构建后明确调用send()receive()函数。