Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:45:57+00:00 2026-06-17T06:45:57+00:00

using namespace std; class Layer { protected: Layer *lower; Layer *upper; public: Layer(Layer *lo,Layer

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

I was trying to simulate a three layer protocol stack using the Protocol Design Pattern. But while dereferencing the upper->receive in DataLink_Layer’s receive i am getting a run time exception: System.AccessViolationException. Why am i getting it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:45:59+00:00Added an answer on June 17, 2026 at 6:45 am

    The constructor of DataLink_Layer is trying to call back into the Application_Layer via a Layer* before the Layer base class of Application_Layer is even constructed (you are still evaluating the new DataLink_Layer(this) at this time).

    You can see this more clearly by simply calling upper->receive() in the DataLink_Layer constructor.

    This FAQ explains a little more about using this in constructors.

    This simpler example may more clearly illustrate the issue:

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

    In general, you shouldn’t use the constructor to execute a complicated call stack on partially constructed objects. Just call the send() or receive() functions explicitly after construction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using namespace std; class WINDOW { protected: GtkWidget *window; public: WINDOW(); GtkWidget* get_window(); void
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include iostream using namespace std; class A { public: void mprint() { cout<<\n TESTING
using namespace std; class A { public: A() {} ~A() {} map<int, string*>& getMap()
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include <iostream> using namespace std; class A { public: A() { cout << Default
using namespace std; class C { int a; public: C(int aa=0) {a=aa;} ~C() {cout
#ifndef SLIST_H #define SLIST_H #include llist.h using namespace std; class slist:public llist{ public: slist();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.