扩展类和实现接口C ++风格 [英] extending classes and implementing interfaces C++ style

查看:53
本文介绍了扩展类和实现接口C ++风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些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

推荐答案

这是我到目前为止所拥有的:

//接口IA

类IA

{

public:

IA(){}

虚拟~IA(){}


void doA()throw()= 0;

}


//接口IB

IB级:公共IA

{

public:

IB(){}

虚拟~IB(){}


void doB()throw()= 0;

}


//符合接口IA的具体类JA

类JA:public IA

{

public:

JA(){}

virtual~JA()


void doA throw(){ ...}

}


//符合接口IB的具体类JB

class JA:public JA,public IB

{

public JB(){}

virtual~JB()


void doB throw(){...}

}


当我编译这个时,我收到一条错误消息,说doA是

类JA中的摘要????

Here is what I have so far:
// interface IA
class IA
{
public:
IA() {}
virtual ~IA() {}

void doA() throw() = 0;
}

// interface IB
class IB: public IA
{
public:
IB() {}
virtual ~IB() {}

void doB() throw() = 0;
}

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() {}
virtual ~JA()

void doA throw() { ... }
}

// concrete class JB conforming to interface IB
class JA: public JA, public IB
{
public JB() {}
virtual ~JB()

void doB throw() { ... }
}

When I compile this, I get an error message saying that doA is
abstract in class JA????



in*****@interec.net napsal:

in*****@interec.net napsal:

这是我到目前为止:


//接口IA

类IA

{

public:

IA(){}

virtual~IA(){}


void doA()throw()= 0;

}


//接口IB

class IB:public IA

{

public:

IB(){}

virtual~IB (){}


void doB()throw()= 0;

}


//具体类JA符合接口IA

class JA:public IA

{

public:

JA(){ }

虚拟~JA()


void doA throw(){...}

}


//符合IB接口的具体类JB

class JA:public JA,public IB

{

public JB(){}

virtual~JB()


void doB throw(){...}

}


当我编译它时,我收到一条错误消息,说明在AA类中,doA是

摘要????
Here is what I have so far:
// interface IA
class IA
{
public:
IA() {}
virtual ~IA() {}

void doA() throw() = 0;
}

// interface IB
class IB: public IA
{
public:
IB() {}
virtual ~IB() {}

void doB() throw() = 0;
}

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() {}
virtual ~JA()

void doA throw() { ... }
}

// concrete class JB conforming to interface IB
class JA: public JA, public IB
{
public JB() {}
virtual ~JB()

void doB throw() { ... }
}

When I compile this, I get an error message saying that doA is
abstract in class JA????



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


在***** @interec.net 中写道:
in*****@interec.net wrote:

我有一些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



假设最后一堂课应该是:

公共级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 ++风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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