给定基类A和派生类B:
class A {
public:
virtual void f() = 0;
};
class B : public A {
public:
void g();
};
void B::g() {
cout << "Yay!";
}
void B::f() {
cout << "Argh!";
}
Run Code Online (Sandbox Code Playgroud)
我得到错误,说在尝试定义void B :: f()时没有在B中声明f().我必须在B中明确声明f()吗?我认为如果接口改变,我不应该纠正从它派生的每个类中的声明.B是否无法自动从A获取所有虚函数的声明?
编辑:我发现一篇文章说纯虚函数的继承取决于编译器:http: //www.objectmentor.com/resources/articles/abcpvf.pdf
我正在使用VC++ 2008,想知道是否有一个选项.
有没有办法限制javac的最大虚拟内存使用?运行java时,我可以使用"-XmxAm"(其中A是可用兆字节数)运行它来限制它.那样的javac有什么吗?谢谢.
模板的早期绑定和虚函数的后期绑定.因此,如果模板包含虚函数,是否安全?
template<typename T>
class base {
public:
T data;
virtual void fn(T t){}
};
Run Code Online (Sandbox Code Playgroud) 您好我正在尝试使用GNU Scientific Library中的微分方程包编写一个小程序来模拟动态系统.问题不是GSL特有的,但我只是给你所有的细节
在当前的设计中,我想要一个抽象Experiment类,其中所有复杂的函数都将由gsl库调用.显式系统的动力学将由两个函数定义,即func和jacob,它们分别定义特定的运动方程和雅可比.因此,我想在Experiment类中进行所有模拟,并且只覆盖具有将由其继承的特定类的两个虚函数Experiment.
我遇到的问题是虚拟这些方法不能编译
error: argument of type ‘int (Experiment::)(double, const double*, double*, void*)’ does not match ‘int (*)(double, const double*, double*, void*)’
如果我将这两个函数设置为静态,程序将编译,但我将失去我想要针对特定问题实现的功能.
显然,它们不能同时是静态的和虚拟的,所以有人知道这个问题的解决方法吗?有什么建议可以更好地接近它吗?
提前致谢.
编辑:下面的代码编译,但他们不是虚拟的
class Experiment
{
public:
Experiment();
~Experiment();
void setupExperiment();
static int func(double t, const double y[], double f[], void *params);
static int jac (double t, const double y[], double *dfdy, double dfdt[], void *params);
};
void Experiment::setupExperiment(){
double mu = 10;
gsl_odeiv2_system sys = {func, jac, 2, …Run Code Online (Sandbox Code Playgroud) 可能重复:
在Objective C中创建一个抽象类
在Java中,我喜欢使用抽象类来确保一堆类具有相同的基本行为,例如:
public abstract class A
{
// this method is seen from outside and will be called by the user
final public void doSomething()
{
// ... here do some logic which is obligatory, e.g. clean up something so that
// the inheriting classes did not have to bother with it
reallyDoIt();
}
// here the actual work is done
protected abstract void reallyDoIt();
}
Run Code Online (Sandbox Code Playgroud)
既然如果B类继承自A类,它只需要实现reallyDoIt().
如何在Objective C中做到这一点?它可能吗?在Objective C中它可行吗?我的意思是整个范式似乎在Objective C中有所不同,例如我从中理解的是没有办法禁止覆盖一个方法(比如Java中的'final')?
谢谢!
经过长时间的C风格程序编码,我才开始"获得"OOP.所以我怀疑可能有标准的方法来处理我所面临的情况.我有一个应用程序,其类层次结构如下所示:
#include <iostream>
using namespace std;
class A {
public:
virtual int intf() { return 0;} // Only needed by B
virtual double df() {return 0.0;} // Only needed by C
};
class B : public A {
int intf() {return 2;}
// B objects have no use for df()
};
class C : public B {
double df() {return 3.14;}
// C objects have no use for intf()
};
int main(){
// Main needs to instantiate both B and …Run Code Online (Sandbox Code Playgroud) 假设我有以下类层次结构:
template< class T >
class TestBase {
public:
virtual T const & do_foo() = 0;
};
template< class T >
class TestDerived : public virtual TestBase< T > {
public:
virtual int do_bar() {
return do_foo() + 1;
}
};
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会吐出以下内容:
error: there are no arguments to ‘do_foo’ that depend on a template parameter, so a declaration of ‘do_foo’ must be available [-fpermissive]
note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an …Run Code Online (Sandbox Code Playgroud) class subscriber
{
public:
virtual void update() = 0;
}
class entity : public subsriber
{
public:
virtual void update() = 0;
}
class myObject : public entity
{
public:
virtual void update()
{
do_things();
}
}
subscriber * ptr = new myObject; //will use shared_ptr, but here i want simplicity
ptr->update();
Run Code Online (Sandbox Code Playgroud)
问题是,是否会调用正确的更新函数(在myObject中实现的函数)?在一个"系列"中有两个具有相同名称的纯虚函数是否有效?
我今天做了一个简单的测试:
struct C{virtual void f()=0;};
void C::f(){printf("weird\n");}
Run Code Online (Sandbox Code Playgroud)
该程序还可以,但是对我来说很奇怪,当我们使用=0它时,意味着应该在继承的类中定义函数体,但是看来我仍然可以给它实现函数。
我尝试了GCC和VC,都可以。因此在我看来,这应该是C ++标准的一部分。
但是,为什么这不是语法错误?
我想到的一个原因是C#同时具有'interface'和'abstract'关键字,interface无法实现,而abstract可能具有某些实现。
令我感到困惑的是,C ++应该支持这种奇怪的语法吗?
当我尝试打开虚拟设备时,会看到相同的错误。我能做什么?
该错误:
Emulator: warning: host doesn't support requested feature: CPUID.80000001H:ECX.abm [bit 5]
Run Code Online (Sandbox Code Playgroud)
我无法解决我的问题:/我昨天问过,但有人回答了:/
注意:已启用虚拟化技术,并且KVM正常。
virtual ×10
c++ ×7
abstract ×2
class ×2
inheritance ×2
android ×1
definition ×1
device ×1
function ×1
javac ×1
memory ×1
methods ×1
objective-c ×1
static ×1
templates ×1
visual-c++ ×1