开发者

pure virtual declarations in subclasses

i have a a couple c++ interfaces like this:

struct IThese {
virtual void doThesethings() = 0;
}

struct IThose : public IThese {
virtual void doThoseOtherThings() = 0;
}

Notice that IThose implement their own method, but also implements those from the other interface, so the idea is that implementors of IThose will need to implement both

Question: do i need to redeclare doThesethings in IThose?

if not, what would happen if i did it? would it shadow the I开发者_如何学GoThese method?


Presently class IThose is abstract class** and you don't have to redeclare doTheseThings() inside it. You can choose to implement doTheseThings() inside class IThose.

If doThesethings() is implemented in class IThose, then it's child class (which derive IThose) may or may not implement it. But they must implement doThoseOtherThings(), if they don't want to be abstract.

**abstract class: contains at least one pure virtual method within it or via its base class


You do not need to redeclare doThesethings() in IThose.

A class (or struct) that inherits from IThese must implement doThesethings().

A class (or struct) that inhertis from IThose must implement both doThesethings() and 'doThoseOtherThings()`.

To answer your other question, if you were to redeclare doThesethings() in IThose, different compilers might react differently. Either it won't have any effect because the compiler will consider it redundant, or it will be an error because a pure virtual method was declared twice and was pure virtual in both cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜