基类调用的覆盖函数? [英] Overriding function called by base class?

查看:140
本文介绍了基类调用的覆盖函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设计为通用的类,用于任何地方,看起来像这样:

I have a class that is designed to be general-purpose, used wherever, that looks kinda like this:

class FixedByteStream {
public:
  FixedByteStream(const char* source)
  {
    size = strlen(source);
    copy(source);
  }

  /* Many more constructors here */

protected:
  void copy(const char* source)
  {
    address = allocate();
    //...
  }

  /* Plus other functions that call allocate() */

  char* FixedByteStream::allocate()
  {
    return (char*)malloc(size);
  }
}



我已经扩展了这个类,使用特定于项目的内存池。

I have then extended this class, so that it can use a project-specific memory pool.

class PooledByteStream : public FixedByteStream {
public:
  PooledByteStream::PooledByteStream() : FixedByteStream() {}

protected:
  char* PooledByteStream::allocate()
  {
    return (char*)PooledByteStream::pool.allocate(size);
  }
}

PooledByteStream应该是 em>到一个FixedByteStream,所有相同的函数和构造函数,除了当allocate()被调用时,它应该从内存池检索指针。

PooledByteStream is supposed to be identical to a FixedByteStream, with all the same functions and constructors, except that when allocate() is called, it should be retrieving pointers from a memory pool.

PooledByteStream :: allocate()不会被调用。不是从继承的构造函数,也不是从其他继承的函数(调用继承的copy())。从基类继承的任何事情完全不了解allocate()应该做不同的事情现在。事实上,我如何解决这个问题呢?

However, PooledByteStream::allocate() is not ever called. Not from the inherited constructors, nor from other inherited functions (that call the inherited copy()). Anything inherited from the base class is completely oblivious to the fact that allocate() ought to do something quite different now.

如何使继承的函数调用重写的函数,而不是基类的那些?从基类中复制 - 粘贴所有必要的函数会删除继承点,所以我假设这里不是答案。

The question is, how do I fix that? How do I make inherited functions call overridden functions, rather than those of the base class? Copy-pasting all the necessary functions from the base class would obliterate the point of inheritance, so I'm assuming that's not the answer here.

注意: / strong>我不是寻求内存管理的建议,或者其他方式达到相同的最终结果。这只是一个例子!

NOTE: I am not looking for advice on memory management, or other ways to reach the same end result. This is just an example!

推荐答案

您需要声明 allocate()为虚拟以覆盖它。然而,基类构造函数不能调用派生类的覆盖,因为派生类还没有被构造,并且基类析构函数不能调用派生类的覆盖,因为派生类已经被销毁。

You need to declare allocate() as virtual in order to override it. However, a base class constructor cannot call a derived class's overrides because the derived class has not been constructed yet, and a base class destructor cannot call a derived class's overrides because the derived class has already be destructed.

如果你必须在基类构造函数中调用 allocate(),你可以使用模板来绕过这个限制,例如:

If you must call allocate() in the base class constructor, you can use a template to get around the restriction, eg:

template<typename Derived>
class FixedByteStreamBase
{
public:
  FixedByteStreamBase(const char* source)
  {
    size = strlen(source);
    copy(source);
  }

  /* Many more constructors here */

protected:
  void copy(const char* source)
  {
    address = Derived::allocate();
    //...
  }

  /* Plus other functions that call allocate() */
};

class FixedByteStream : public FixedByteStreamBase<FixedByteStream>
{
public:
    static char* allocate()
    {
        return (char*)malloc(size);
    }
};

class PooledByteStream : public FixedByteStreamBase<PooledByteStream>
{
public:
    static char* allocate()
    {
        return (char*)pool.malloc(size);
    }
};

或:

struct MallocAllocator
{
    static char* allocate()
    {
        return (char*)malloc(size);
    }
};

struct PoolAllocator
{
    static char* allocate()
    {
        return (char*)pool.allocate(size);
    }
};

template<typename Allocator>
class FixedByteStreamBase {
public:
  FixedByteStreamBase(const char* source)
  {
    size = strlen(source);
    copy(source);
  }

  /* Many more constructors here */

protected:
  void copy(const char* source)
  {
    address = Allocator::allocate();
    //...
  }

  /* Plus other functions that call allocate() */
};

typedef FixedByteStreamBase<MallocAllocator> FixedByteStream;
typedef FixedByteStreamBase<PoolAllocator> PooledByteStream;

这篇关于基类调用的覆盖函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆