Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extending classes and implementing interfaces C++ style

Reply
Thread Tools

extending classes and implementing interfaces C++ style

 
 
Grizlyk
Guest
Posts: n/a
 
      02-01-2007
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


 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      02-01-2007
Pete Becker wrote:
> Grizlyk wrote:
>>
>> template< class IB>
>> class JB: public interface_JB
>> {
>> private:
>> IB ib;
>> }
>>

>
> The goal is to be able to pass objects of the derived type to a function
> like this:
>
> void f(const IB&);
>
> Using IB to define a member doesn't support that.
>



No, I was assuming "IB" is derived from "interface_JB". Look here
news:epsoea$ghu$ (message dated "16:03")

--
Maksim A Polyanin


 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      02-01-2007
Alan Johnson wrote:
>
> Where did anybody use private inheritance?
>


Here

>> // concrete class JB conforming to interface IB
>> class JB: public JA, public IB


"public JA" is really "inheritance of implementation".

--
Maksim A Polyanin


 
Reply With Quote
 
Dave Rahardja
Guest
Posts: n/a
 
      02-02-2007
On 31 Jan 2007 21:51:01 -0800, wrote:

>I have some code in Java that I need to translate into C++. My Java
>code defines a class hierarchy as follows:


In general, Java's interface definitions can be implemented in C++ as a class
with nothing but pure virtual functions (and a public virtual destructor that
does nothing).

Java:

public interface I
{
public abstract void fn();
}

C++:

class I // Interface
{
public:
virtual void fn() = 0;
virtual ~I() {}
};

Implementing a Java interface is analogous to virtual public inheritance in
C++.

Java:

public class A implements I
{
public void fn() { /* ... */ }
}

C++:

class A: public virtual I
{
public:
virtual void fn() { /* ... */ }
};


I started a thread about the pros and cons of performing this analogous
exercise in C++ about a week ago. Do a search for my name and the topic
"Virtual inheritance and interfaces".

-dr
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring and implementing exceptions inside interfaces? josh Java 6 12-19-2006 08:29 AM
Style question on abstract classes that implement interfaces c_kernel@hotmail.com Java 2 10-11-2006 04:16 AM
new style and classic style classes ? ankit Python 1 12-22-2005 08:34 AM
Implementing Interfaces and Type Safety (OOP Newbie in Perl) Veli-Pekka Tätilä Perl Misc 7 08-20-2005 10:02 AM
Conditionally implementing __iter__ in new style classes Thomas Heller Python 13 07-08-2005 06:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57