问题
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 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.
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.
NOTE: I am not looking for advice on memory management, or other ways to reach the same end result. This is just an example!
回答1:
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.
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);
}
};
Or:
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;
来源:https://stackoverflow.com/questions/14290132/overriding-function-called-by-base-class