Alan Johnson wrote:
>>>// concrete class JA conforming to interface IA
public class JA implements IA
Wrong way in C++. Do not use private inheritance in C++ without concrete
request, because it is nearly always much better to use composition here:
template< class IA>
class JA
{
private:
IA ia;
}
Where did anybody use private inheritance?
Yes, i forget, that Java directives "class JA implements IA" does not mean
"private inheritance". Look my message dated "12:45":
>Grizlyk wrote:
Maybe i was wrong with Java directives, but i hope my reasons are clear -
use composition instead of C++ multiple inheritance
It means, that independent from Java directives, the Java classes can be
implemented without multiple inheritance.
Runtime polymorphism can't be achieved with templates and composition in
C++.
What do you mean as "runtime polymorphism"? The following expression
>public class JB extends JB implements IB
can not be treated as "runtime polymorphism" (for C++ at least), because
"class JB " will be created at compile time or am I wrong?
>>>How do I translate these classes into C++
As I could understand, the problem is appearence of multiple inheritence for
direct Java -C++ casting and OP wants to avoid it (take in account, I can
not see whole thread), if yes:
1. In all cases any kind (ordinary or multiple) of "inheritance of
implementation" _can_ be replaced by composition (static or dynamic).
2. In most cases any kind (ordinary or multiple) of "inheritance of
implementation" _must_ be replaced by composition (static or dynamic),
because composition (instead of inheritance) increase ability of "reusage"
of classes.
Somebody implements the Java classes on C++ like this:
>>
// interface IA
class IA
{
public:
virtual void doA() throw() = 0;
};
// interface IB
class IB: public IA
{
public:
virtual 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 JB: public JA, public IB
Here he has used multiple inheritance.
>In your example, what is interface_JB?
In the example "interface_JB" is "IB".
This is my example of static implementation with the help of composition
template<class IA=JA>
class JB: public IB
{
IA ia;
public:
//forwarding
void doA() throw() {ia.doA();}
//to implement
void doB() throw();//{ia.doA();}
};
This is my example of dynamic implementation with the help of composition
template< class IA>
class JB: public IB
{
private:
IA *ia;
public:
//forwarding
void doA() throw() {ia->doA();}
//to implement
void doB() throw();//{ia->doA();}
public:
JB( IA *p):ia(p){}
};
Note, that explicit throw specification (as "throw(expr)") is not safe in
current C++ implementation, in the following example:
void doA() throw() { throw 0; }
try{ doA(); }catch(...){}
"throw 0;" will terminate your program.
--
Maksim A Polyanin