Answer the question
In order to leave comments, you need to log in
Why create a constructor in an abstract base class?
Hello!
In the literature that I read, the author creates a constructor in ABC:
class ABC{
public:
ABC(tralala &, trololo);
....
};
class Test : public ABC{
public:
Test(trololo, tututu &):ABC(tututu &, trololo);
};
Answer the question
In order to leave comments, you need to log in
Abstract classes are divided into interfaces and partially implemented. The line between them is as follows:
• The interface has no data.
• For an interface, all non-abstract virtual methods represent either the reference behavior or the most common implementation. In both cases, if anything, they should not be expanded, but rewritten from scratch.
So, for interfaces, such constructors, of course, are not necessary.
For example, between an abstract stream and a Win32 file, there can be such a hierarchy: Stream → HandleStream → File. Stream is an interface, even if there is something like
// virtual
unsigned long long Stream::remainder() const { return size() - pos(); }
HandleStream::HandleStream(HANDLE aHandle) : fHandle(aHandle) {}
HandleStream::~HandleStream() { close(); }
void HandleStream::close()
{
if (Handle != INVALID_HANDLE) { // не помню, как там эта константа в Win32
CloseHandle(fHandle);
fHandle = INVALID_HANDLE;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question