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 502871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:20:09+00:00 2026-05-13T06:20:09+00:00

Suppose I have a class Base which has a member variable A* my_hash. I

  • 0

Suppose I have a class Base which has a member variable A* my_hash.
I also have class Extended which inherits from class Base. I also have a class B
which extends A.

class Base{
  Base(): my_hash(new A) {}
  //methods which use my_hash
protected:
  A* my_hash;

};

class Extended:public Base{
 //methods which use my_hash from A
 //I cannot have a B* my_other_hash in this class
 //I would like to substitute B* my_hash
 //I cannot let Base create my_hash (of type A*) because that is not what I want.
};

I would like Extended to do the usual (i.e. use everything it inherits from A), except
and with one important difference, I want my_hash to be B* instead of A*.
Whenever something accesses my_hash, either via Extended’s methods or Base’s methods,
I would like the methods to be executed to be B*’s.

One thing to try:
I cannot have a method call (e.g. create_hash() in Base()) which I redefine in Extended.
This does not work as there seems no way to go back up to the class Extended when I create the hash.

I would not like Base to even know about B. How do I do this?

  • 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-05-13T06:20:09+00:00Added an answer on May 13, 2026 at 6:20 am

    If the type of ‘B’ extends ‘A’, then you could set it up so that you pass the value for ‘m_hash’ through the constructor (you can also hide this constructor as protected so code that doesn’t inherit from ‘Base’ can’t extend it).

    e.g.

    class Base{
      Base(): my_hash(new A) {}
      //methods which use my_hash
    protected:
    
      A* my_hash;
      Base(A* hash): my_hash(hash) {}
    };
    
    class Extended:public Base{
    public:
      Extended() : Base(new B) {}
    };
    

    Also, if you want new, specialised functions in ‘B’ that you can call from ‘Extended’, then you can either store that in another pointer or just cast ‘my_hash’ to type ‘B*’.

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

Sidebar

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.