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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:35:42+00:00 2026-05-23T02:35:42+00:00

I’m having a class with 2 pure virtual methods and another class which needs

  • 0

I’m having a class with 2 pure virtual methods and another class which needs to use an object of this class. I want to allow the user of this class to specify which derivation of the abstract class should be used inside of it.
I’m struggling to figure out what the right way is.

struct abstract {
    virtual int fst_func() = 0;
    virtual void sec_func(int) = 0;
};

// store an instance of "abstract".
class user_of_abstract
{
private:
    abstract* m_abstract;

public:
    // Pass a pointer to an "abstract" object. The caller takes care of the memory resource.
    user_of_abstract_base(abstract* a) : m_abstract(a) { }

    // Pase any type, which needs to be derived from "abstract" and create a copy. Free memory in destructor.
    template<class abstract_type>
    user_of_abstract_base(abstract_type const& a) : m_abstract(new abstract_type(a)) { }

    // use the stored member to call fst_func.
    int use_fst_func() {
        return this->m_abstract->fst_func();
    }

    // use the stored member to call sec_func.
    void use_sec_func(int x) {
        this->m_abstract->sec_func(x);
    }
};

// use boost::shared_ptr
class user_of_abstract
{
private:
    boost::shared_ptr<abstract> m_abstract;

public:
    // Pass a pointer to an "abstract" object. The caller takes care of the memory resource.
    user_of_abstract_base(boost::shared_ptr<abstract> a) : m_abstract(a) { }

    // use the stored member to call fst_func.
    int use_fst_func() {
        return this->m_abstract->fst_func();
    }

    // use the stored member to call sec_func.
    void use_sec_func(int x) {
        this->m_abstract->sec_func(x);
    }
};

// pass a pointer of an "abstract" object wherever needed.
struct user_of_abstract
{
    // use the passed pointer to an "abstract" object to call fst_func.
    int use_fst_func(abstract* a) {
        return a->fst_func();
    }

    // use the passed pointer to an "abstract" object to call sec_func.
    void use_sec_func(abstract* a, int x) {
        a->sec_func(x);
    }
};

It’s important to note that parameter “x” from sec_func() needs to be a value returned by fst_func() on the same “abstract” instance.

EDIT:
Added another approach using boost::shared_ptr which should take the most advantages.

  • 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-23T02:35:42+00:00Added an answer on May 23, 2026 at 2:35 am

    I would say that passing the abstract object into the constructor of your user is the proper approach as the methods of the user depend being called on the same abstract object. I would even go further and make the x parameter an internal state of your user as you have said it’s important that this value is the one returned from a call from the first function.

    Update: If you are worried about the lifetimes then you could make use of the various smart pointer options available in for example boost. Those should cover most usage scenarios.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.