不知道这个名字是不是很能说明问题,但还是来了。
我正在与一个需要继承基类和定义许多纯虚方法的API接口。
为了良好的编程实践,我希望在不同的(子)类中定义这些方法。
目前,我使用一个facade/包装器(两者兼而有之)类,它继承基类,实例化子类,并调用这些实例化类的必要方法:
#include <cstdio>
class Base
{
public:
virtual void reqImplementation( void ) = 0;
};
class APIImplementation
{
private:
Base * ptr_;
public:
APIImplementation( Base * ptr ) :
ptr_( ptr )
{
ptr_->reqImplementation();
}
};
class MyImplementation
{
private:
APIImplementation * api_;
public:
void reqImplementation( void )
{
printf("Hello World!\n");
}
MyImplementation( APIImplementation * api ) : api_( api ) {}
};
class MyFacade : public Base
{
private:
MyImplementation * my_impl_;
APIImplementation * api_;
void reqImplementation( void )
{
my_impl_->reqImplementation();
}
public:
MyFacade( void )
{
api_ = new APIImplementation( this );
my_impl_ = new MyImplementation( api_ );
}
};
int main( void )
{
MyFacade my_facade;
return 0;
}
有没有方法在这个facade/包装器中实例化的子类中实现纯虚拟函数?或者,对于这样的事情,什么是好的做法?我想要类似的东西(我知道它现在显然不起作用):
#include <cstdio>
class Base
{
public:
virtual void reqImplementation( void ) = 0;
};
class APIImplementation
{
private:
Base * ptr_;
public:
APIImplementation( Base * ptr ) :
ptr_( ptr )
{
ptr_->reqImplementation();
}
};
class MyImplementation : public Base
{
private:
APIImplementation * api_;
public:
void reqImplementation( void )
{
printf("Hello World!\n");
}
MyImplementation( APIImplementation * api ) : api_( api ) {}
};
class MyFacade : public Base
{
private:
MyImplementation * my_impl_;
APIImplementation * api_;
public:
MyFacade( void )
{
api_ = new APIImplementation( this );
my_impl_ = new MyImplementation( api_ );
}
};
int main( void )
{
MyFacade my_facade;
return 0;
}
请注意,API源代码是开放的,因此我可以将这些纯虚拟方法更改为其他任何东西,但是代码非常详尽,因此我宁愿进行小的更改而不是重大的更改。
如果我猜对了,你需要一个虚拟继承。下面是一个示例
#include <iostream>
using namespace std;
// base class with lots of methods (two, actually)
struct Base {
virtual void f() = 0;
virtual void g() = 0;
};
// here's a class with implementation of f
struct ImplementationPart1 : public virtual Base {
virtual void f() {
cout << 4;
}
};
// here's a class with implementation of g
struct ImplementationPart2 : public virtual Base {
virtual void g() {
cout << 2;
}
};
// here's a class with all the implementations
struct Implementation : public ImplementationPart1, public ImplementationPart2 {};
int main() {
// Implementation inherits from Base, yes
Base *x = new Implementation();
// everything works, as expected
x->f();
x->g();
}
活生生的例子。
你的网站最终可能要用多台服务器提供的服务,模拟这种环境你可以在本地项目里定义多台虚拟机。在 Vagrant 配置文件(Vagrantfile)里,可以定义多台虚拟机,每台虚拟机都有自己的名字,各自独立的配置。比如你可以单独设置每台虚拟机的 IP 地址。你也可以单独控制某台虚拟机。 下面这个配置定义了两台虚拟机,web 与 db: Vagrant.configure("2") do |config|
我知道在C++中没有什么比虚拟模板方法更好的了,但似乎它正是我所需要的。有什么办法可以让我使用吗?我很感谢任何建议。 我想通过add方法将实体添加到向量中,这些实体需要是虚拟的,也需要模板,如何避免这种情况?
预先定义的虚拟服务器 FreeRADIUS包括站点可用子目录下的虚拟服务器。有些可以按原样使用,而有些则是用于特殊要求的模板。以下是一些虚拟服务器: buffered-sql:此虚拟服务器用于克服大型SQL数据库(type = detail)的速度限制。 copy-acct-to-home-server:此虚拟服务器可用作模板,用于在两个位置记录一个计费请求(type = detail)。 coa
我安装的虚拟环境使用()。这就出现了。
我对虚拟函数感到困惑。有人告诉我,父类中的虚拟意味着我可以在子类中覆盖它。但是,如果我省略父类中的虚拟,我仍然可以覆盖它。
本文向大家介绍C#处理datagridview虚拟模式的方法,包括了C#处理datagridview虚拟模式的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#处理datagridview虚拟模式的方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的C#程序设计有所帮助。