问题描述
我有一些Java代码需要翻译成C ++。我的Java
代码定义了一个类层次结构如下:
//接口IA
公共接口IA
{
public abstract void doA();
}
//接口IB
公共接口IB扩展接口IA
{
public abstract void doB();
}
//符合接口IA的具体类JA
公共类JA实现IA
{
public void doA(){.. 。$
}
//从JA扩展的具体类JB(这样JB知道方法
doA())并且
//符合接口IB(源自接口IA)
公共类JB扩展JB实现IB
{
public void doB(){...}
}
如何将这些类翻译成C ++(特别是JB类),
没有在JA和JB的C ++等价类中重复代码和
JA和JB符合他们的尊重
接口IA和IB。
谢谢
- Albert
网站管理员,InterEC.NET
I have some code in Java that I need to translate into C++. My Java
code defines a class hierarchy as follows:
// interface IA
public interface IA
{
public abstract void doA();
}
// interface IB
public interface IB extends interface IA
{
public abstract void doB();
}
// concrete class JA conforming to interface IA
public class JA implements IA
{
public void doA() { ... }
}
// concrete class JB extending from JA (so that JB knows about method
doA() ) and
// conforms to interface IB (which is derived from interface IA)
public class JB extends JB implements IB
{
public void doB() { ...}
}
How do I translate these classes into C++ (specially class JB),
without duplicating code in C++ equivalent classes of JA and JB and
while making sure that JA and JB conform to their respecting
interfaces IA and IB.
Thanks
-- Albert
Webmaster, InterEC.NET
推荐答案
类JA:公共JA,公共IB是无稽之谈(我认为这是错字) -
类不能继承自己。
doA是抽象的,因为它被声明为void doA()throw()= 0;,
但是我想念关键字virtual(在C ++中不是所有方法虚拟
和其他一些语言一样)。
class JA: public JA, public IB is nonsense (I think it is typo) -
class cannot inherit from itself.
doA is abstract, because it is declared as void doA() throw() = 0;,
but I miss there key word virtual (in C++ are not all methods virtual
as in some other languages).
假设最后一堂课应该是:
公共级JB扩展JA工具IB
我认为这就是你想要的。
class IA
{
public:
virtual~IA(){}
virtual void doA()= 0;
};
IB类:虚拟公共IA
{
public:
virtual void doB()= 0 ;
};
类JA:虚拟公众IA
{
public:
虚拟无效doA()
{
std :: cout<< " JA :: DOA()" << std :: endl;
}
};
类JB:公共JA,公共IB
{
public:
virtual void doB()
{
std :: cout<< ; " JB :: DOB()" << std :: endl;
}
};
一些注释:
虚拟关键字在我使用它的任何地方都不需要。一旦你在基类中创建了虚拟的
a函数,它就会在派生的
类中保持虚拟,无论你是否声明它。
虚拟在虚拟继承中并不意味着与
虚拟相同的东西。在虚拟成员函数中。如果您还没有理解
虚拟继承,我建议您查看它,而不是通过假设它的作用而自己混淆
。另外,请参阅Dave
Rahardja今天早些时候的帖子Virtual Inheritance and
interfaces关于它如何适用于你的情况的一些辩论。
IA中的虚拟析构函数不是必需的,但它是一个好主意。
另外,对于为什么你应该在一个没有内联定义的类中至少有一个
虚函数,有一些很好的论据。由于IA的唯一函数是具有定义的唯一函数,因此可能不会像我那样将其内联定义。
-
Alan Johnson
Assuming that last class was supposed to be:
public class JB extends JA implements IB
I think this is what you want.
class IA
{
public:
virtual ~IA() {}
virtual void doA() = 0 ;
} ;
class IB : virtual public IA
{
public:
virtual void doB() = 0 ;
} ;
class JA : virtual public IA
{
public:
virtual void doA()
{
std::cout << "JA::doA()" << std::endl ;
}
} ;
class JB : public JA, public IB
{
public:
virtual void doB()
{
std::cout << "JB::doB()" << std::endl ;
}
} ;
Some notes:
The virtual keyword is not required everywhere I used it. Once you make
a function virtual in a base class, it remains virtual in derived
classes whether you declare it as such or not.
The "virtual" in virtual inheritance doesn''t mean the same thing as the
"virtual" in virtual member functions. If you don''t already understand
virtual inheritance, I suggest looking it up rather than confusing
yourself by making assumptions about what it does. Also, see Dave
Rahardja''s thread from earlier today "Virtual Inheritance and
interfaces" for some debate about how it applies to your situation.
The virtual destructor in IA isn''t required, but it is a Good Idea.
Also, there are some good arguments for why you should have at least one
virtual function in a class that is not defined inline. Since the
destructor of IA is the only function with a definition, it might make
sense not to define it inline as I did.
--
Alan Johnson
这篇关于扩展类和实现接口C ++风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!