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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:13:41+00:00 2026-05-17T03:13:41+00:00

I have an existing application in C++ with a custom ArrayBase class that manages

  • 0

I have an existing application in C++ with a custom ArrayBase class that manages storage and access to a contiguously allocated region of memory. I have a separate ItrBase class that is used to access data in that ArrayBase. ArrayBase has a createItr() function that currently returns an ItrBase object.

I need to extend ArrayBase to use multiple memory allocations instead of one contiguous one. I have created an EnhancedArray class to do that. For this EnhancedArray to be compatible with the existing application, it’s createItr() function must return something that works with the new multiple memory allocations.

So, I have created a derived EnhanceItr class to do this.
My problem is I can’t figure out a way for hundreds of code occurrences like this:

ItrBase anIterator = anArray.createItr();
...
double x = anIterator.getData();

to use the EhancedItr‘s getData() function when anArray is an EnhancedArray.

Here is a simple application illustrating my basic arrangement.

#include <iostream>
using namespace std;

class ItrBase {
public:
 ItrBase() { cout << "ItrBase constructor.\n"; };
 ~ItrBase() { cout << "ItrBase destructor.\n"; };
 virtual int vfunc() {return 1;};
};


class EnhancedItr : public ItrBase {
public:
 EnhancedItr() { cout << "EnhancedItr constructor.\n"; };
 ~EnhancedItr() { cout << "EnhancedItr destructor.\n"; };
 int vfunc() {return 0;};
};


class ArrayBase {
public:
 ArrayBase() { cout << "ArrayBase constructor.\n"; };
 ~ArrayBase() { cout << "ArrayBase destructor.\n"; };
 virtual ItrBase & createItr() {cout << "in AB's createItr()\n"; return *new ItrBase(); };
};


class EnhancedArray : public ArrayBase {
public:
 EnhancedArray() { cout << "EnhancedArray constructor.\n"; };
 ~EnhancedArray() { cout << "EnhancedArray destructor.\n"; };
 EnhancedItr & createItr() {cout << "in EA's createItr()\n"; return *new EnhancedItr(); };
};


int main()
{
 ArrayBase ab;
 EnhancedArray ea;

 ItrBase itr = ab.createItr();  
 ItrBase eitr = ea.createItr();  //EnhancedItr assigned to ItrBase

 cout << "ArrayBase's Itr .vfunc(): " << itr.vfunc() <<std::endl;
 cout << "EnhancedArray's Itr .vfunc(): " << eitr.vfunc() <<std::endl;
 return 0;
}

Both calls to vfunc() above return 1, when I want the second call to return 0.

In main(), I know that if I change the ItrBase types to ItrBase &‘s, I do get the desired return types, but then I am modifying my ‘existing’ code in hundreds of areas, and the destructors for the Iterators are not called.

Is there another strategy that I am not seeing?

Thanks.

  • 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-17T03:13:41+00:00Added an answer on May 17, 2026 at 3:13 am

    Sure, if you’re allowed to rewrite ItrBase, then you can use delegation to pass all function calls through to an implementation class, which you hold by pointer or reference so that polymorphism is in effect. This would look a lot like pimpl. And the callers would not have to be written at all, only recompiled.

    EDIT: code for those not familiar with pimpl.

    struct ItrBase
    {
      struct ItrImpl
      {
        virtual ~ItrImpl(){}
        virtual int vfunc() = 0;
      };
    
      ItrBase(ItrImpl peer) : m_peer(peer) { cout << "ItrBase constructor.\n"; }
      ~ItrBase() { cout << "ItrBase destructor.\n"; }
      int vfunc() { return m_peer->vfunc(); }
    private:
      const unique_ptr<ItrImpl> m_peer;
    };
    
    class ArrayBase
    {
      struct ItrImpl : public ItrBase::ItrImpl
      {
        virtual int vfunc() { return 0; }
      };
    
    public:
      ArrayBase() { cout << "ArrayBase constructor.\n"; };
      ~ArrayBase() { cout << "ArrayBase destructor.\n"; };
      virtual ItrBase createItr() { cout << "in AB's createItr()\n"; return ItrBase(new ItrImpl); };
    };
    
    class EnhancedArray : public ArrayBase
    {
      struct ItrImpl : public ItrBase::ItrImpl
      {
        virtual int vfunc() { return 1; }
      };
    
    public:
      EnhancedArray() { cout << "EnhancedArray constructor.\n"; };
      ~EnhancedArray() { cout << "EnhancedArray destructor.\n"; };
      virtual ItrBase createItr() { cout << "in EA's createItr()\n"; return ItrBase(new ItrImpl); };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom HttpModule rewrite engine in an existing web application project that
We have an existing application that relies heavily on stored procedures and untyped DataSets.
I have an existing application that imports data from a web site api into
I have an existing 32-bit ASP.NET application that used 32-bit unmanaged DLLs. If I
I have an ASP.NET MVC web application that implements a custom membership provider. The
I have built a custom administration panel into a Rails application that allows content
I have customer that uses older, custom built ERP application for which they don't
I have an existing .NET 3.5 based framework that is extended using custom plugins.
We have an existing Web application and we want to migrate from a custom
I have a custom application that was built to send opt-in newsletters and marketing

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.