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

  • SEARCH
  • Home
  • 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 9200259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:43:33+00:00 2026-06-17T22:43:33+00:00

I am probably missing something very simple, but I’ll be very grateful if someone

  • 0

I am probably missing something very simple, but I’ll be very grateful if someone can point me in the right direction.

I have 2 classes: A and B. A has a collection of data structures and functions that modify them (for a hypothetical example, say add, delete, insert, etc to the data structures). B has a collection of functions that specify how the data structures are modified in A.

For the sake of extensibility, B is inherited to reflect different possible behaviors on A’s data structures. One implementation of B may choose to add to the beginning of A’s data structure, delete from the end, and so on. Another implementation might add to the end, delete from the beginning, and so on.

I am passing an object of base class B during the creation of A, so that the generic addNode(), deleteNode(), and other functions of A can actually look up the particular type of B object passed to it during creation and refer to its implementation in B as to what action to follow.

In this hypothetical example below, the data structure in concern is a doubly linked list. In my problem, the data structure itself is not the issue. I am concerned about how to call the correct method in derived class of B to modify my data structure in A.

Class A:

class A
{
    static A create(B bObj);  // returns an A object after calling the constructor
    void addNode(int);
    void deleteNode();
    ...
private:
    struct myNode
    {
        int value;
        myNode *next;
        myNode *prev;
    }
    myNode *head;
    myNode *tail;
}

Class B:

class B
{
    virtual void addNode(int);
    virtual void deleteNode();
    ...
}

Class ChildB1:

class ChildB1
{
    void addNode(int)
    {
        // code for add to the front
    }
    void deleteNode()
    {
        // code for delete from the back
    }
}

Class ChildB2:

class ChildB2
{
    void addNode(int)
    {
        // code for add to the back
    }
    void deleteNode()
    {
        // code for delete from the front
    }
}

The problem I am running into is how to give a child class of B access to the current object of A I am operating on? Let’s say, while creation of the object of A (aObj) with the create() method, I passed a reference to object ChildB2.

B *bObj = new ChildB2();
A aObj = A.create(*bObj);

Now when I call aObj.add(), I want it to call the add() method of ChildB2 class, so that it can operate on the linked list in A and add it to the back end of the list. But how does the add() function of ChildB2 class access the linked list of aObj?

I apologize if the question is long-winded and naive. I am happy to give further clarification if required.

  • 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-17T22:43:34+00:00Added an answer on June 17, 2026 at 10:43 pm

    Add B as a friend of A.

    class A
    {
       friend class B;
       // the rest
    };
    

    Since friendship is not inherited, B has to explicitly provide its heirs with access to A private parts.

    class B
    {
      protected:
        // since A::myNode is private, redeclare it here
        // for heirs
        typedef A::myNode myNode;
    
        // access to A parts
        void getAParts (A*, myNode*** head, myNode*** tail)
        {
          *head = &a->head;
          *tail = &a->tail;
        }
        // the rest
    
    };
    

    A couple of notes: you should pass B to create by reference, not by value, otherwise your ChildB2 object will be sliced. You also need to manage Bs lifetime somehow.

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

Sidebar

Related Questions

I am probably missing something very simple, but here is the situation: I have
I'm probably missing something very simple but I'm new and it's just easier to
I probably am missing something very simple here, but I cannot see it. MSAccess
I'm probably missing something very simple here, but please bear with me. What I
I'm probably missing something very basic but I cannot figure out why I get
I know I'm probably missing something easy, but I have a foreach loop and
Probably I'm just missing something obvious, but I can't get the image in my
This is a very simple (and probably dumb) question, but I can't seem to
I'm using Visual Studio 2008 Pro. I'm probably missing something very obvious here, but
This is probably something very obvious, but I have a new to Qt and

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.