I've a current application in C++ having a custom ArrayBase class that handles storage and use of a contiguously allotted region of memory. I've got a separate ItrBase class that's accustomed to access data for the reason that ArrayBase. ArrayBase includes a createItr() function that presently returns an ItrBase object.

I have to extend ArrayBase to make use of multiple memory allocations rather than one contiguous one. I've produced an EnhancedArray class to achieve that. With this EnhancedArray to become suitable for the present application, it's createItr() function must return something which works together with the brand new multiple memory allocations.

So, I've produced a derived EnhanceItr class to get this done. My issue is I can not discover a method for 100s of code occurrences such as this:

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

to make use of the EhancedItr's getData() function when anArray is definitely an EnhancedArray.

This is a simple application illustrating my fundamental 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, after i want the 2nd call to come back .

In main(), I understand when I alter the ItrBase types to ItrBase &'s, I actually do obtain the preferred return types, however I'm modifying my 'existing' code in 100s of areas, and also the destructors for that Iterators aren't known as.

Can there be another strategy that i'm not seeing?

Thanks.

Sure, if you are permitted to rewrite ItrBase, you'll be able to use delegation to pass through all function calls right through to an implementation class, that you simply hold by pointer or reference to ensure that polymorphism is within effect. This could look nearly the same as pimpl. And also the phone callers will not have to become written whatsoever, only recompiled.

EDIT: code for individuals unfamiliar 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); };
};