I want make a SharedInterrupt class(B) which will serve in series many objects in one interrupt vector. But the derived classes(C..) of SharedInterrupt class(B) must have same ISR() function(func) as SingleInterrupt class(A). Is it possible?
class A {
public:
virtual void func() = 0;
};
class B : public A {
private:
void func() // I want to hide this method from derived classes (C, etc.)
{
// some code
}
public:
virtual void func() = 0; // And I want to keep A's interface, but compiler error!
};
class C : public B {
public:
void func() {
// some other code
}
};
P.S. Now I have only one idea to hide, make intermediate subclass from B to hide A::func() and inherits C from it.
You can try something using inheritance access specifiers. If you don't know how it works, look at this:
Composition, rather than inhertance, is the only way to get this right in C++.
This pattern uses an abstract base class (
IStream
) that defines the interface. A common instance of this problem is when trying to implement an "OnRead" callback on both a socket class, and later an SSL wrapper, and perhaps later, some kind of application layer "packet" is ready.An implementation might look like this - note the extra bonus advantage of this approach is the ability to swap out parts of the chain that might be optional (so, in the case of a communications class as presented, one can plug in optional compression or encryption modules).