如何隐藏虚拟方法在C ++和保持接口? [英] How to hide virtual method in C++ and keep interface?

查看:154
本文介绍了如何隐藏虚拟方法在C ++和保持接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个SharedInterrupt类(B),它将在一个中断向量中串联多个对象。但是SharedInterrupt类(B)的派生类(C ..)必须具有与SingleInterrupt类(A)相同的ISR()函数(func)。
是可能吗?

  class A {
public:
virtual void func = 0;
};

class B:public A {
private:
void func()//我想从派生类(C等)中隐藏这个方法
{
//一些代码
}

public:
virtual void func()= 0; //我想保留A的接口,但编译器报错!

};

class C:public B {
public:
void func(){
// some other code
}

};

现在我只有一个想法隐藏,使中间子类从B隐藏A :: func()并从它继承C.

解决方案

这种模式使用一个抽象基类( IStream)。

)定义接口。这个问题的一个常见实例是当试图在套接字类和稍后的SSL包装器上实现OnRead回调时,或许以后某种类型的应用层数据包就绪了。



一个实现可能看起来像这样 - 注意这种方法的额外的奖励优势是能够交换掉可能是可选的链的部分(因此,在通信类的情况下,可以插入可选的压缩或加密模块)。

  struct IStream {
virtual OnRead()= 0;
};

class Socket:IStream {
IStream * _delegate;
protected:
virtual OnRead(){
while(ReadBytes())
_delegate-> OnRead();
}
public:
Socket(IStream * delegate):_ delegate(delegate){}
};

类SSLSocket:IStream {
Socket * _socket;
IStream * _delegate
protected:
virtual OnRead(){
while(ReadSslRecord())
_delegate-> OnRead
}
public:
SslSocket(IStream * delegate):_ delegate(delegate),_ socket(new Socket(this)){}
};

class AppSocket:IStream {
IStream * _socket;
public:
AppSocket(bool useSsl){
_socket = useSsl?new SslSocket(this):new Socket(this);
}
}


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.

解决方案

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).

struct IStream {
  virtual OnRead()=0;
};

class Socket : IStream {
  IStream* _delegate;
protected:
  virtual OnRead(){
    while(ReadBytes())
      _delegate->OnRead();
  }
public:
  Socket(IStream* delegate):_delegate(delegate){}
};

class SSLSocket : IStream {
  Socket* _socket;
  IStream* _delegate;
protected:
  virtual OnRead(){
    while(ReadSslRecord())
      _delegate->OnRead();
  }
 public:
  SslSocket(IStream* delegate):_delegate(delegate),_socket(new Socket(this)){}
};

class AppSocket : IStream {
  IStream* _socket;
public:
  AppSocket(bool useSsl){
    _socket = useSsl?new SslSocket(this):new Socket(this);
  }
}

这篇关于如何隐藏虚拟方法在C ++和保持接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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