473,664 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixing interface and functional inheritance

Below is a code snippet that fails to compile under vc.net

class BaseInterface
{
public:
virtual void f() = 0;
};

class BaseImplementat ion : public BaseInterface
{
public:
void f(){}
};

class ExtraInterface : public BaseInterface
{
virtual void g() = 0;
};

class ExtraImplementa tion: public BaseImplementat ion, public
ExtraInterface
{
void g(){}
};

void func()
{
ExtraImplementa tion d;
}

The compiler complains that ExtraImplementa tion is an abstract class
and therefore cannot be initialized. Function f, despite being
defined in BaseImplementat ion, is undefined in ExtraImplementa tion.

I hope it's clear what I'm trying to do, i.e., maintaining two
inheritance lines that parallel each other. One line consists of
interfaces and the other implementation. Am I misguided in my
efforts?

Kevin
Jul 19 '05 #1
4 2573
On 22 Sep 2003 17:39:26 -0700, in*****@hotmail .com (Kevin L) wrote:
Below is a code snippet that fails to compile under vc.net

class BaseInterface
{
public:
virtual void f() = 0;
};

class BaseImplementat ion : public BaseInterface
{
public:
void f(){}
};

class ExtraInterface : public BaseInterface
{
-- public:

virtual void g() = 0;
};

class ExtraImplementa tion: public BaseImplementat ion, public
ExtraInterfa ce
{
-- public:

void g(){}
};

void func()
{
ExtraImplementa tion d;
}

The compiler complains that ExtraImplementa tion is an abstract class
and therefore cannot be initialized. Function f, despite being
defined in BaseImplementat ion, is undefined in ExtraImplementa tion.
The compiler is correct.

In Java you can inherit a function spec from an interface, and an
implementation of that function from a concrete class. In C++ the
two inheritance chains are distinct, functions in one don't map
onto functions in the other, unless you use virtual inheritance,
which is both messy and a tad inefficient.

Instead of using virtual inheritance (the tech level solution) I
recommend _factoring out_ the ExtraInterface, like this:
class ExtraInterface
{
public:
virtual void g() = 0;
};
Now isn't that nice?

I hope it's clear what I'm trying to do, i.e., maintaining two
inheritance lines that parallel each other. One line consists of
interfaces and the other implementation.
You can do that as shown above.

I do not recommend the virtual inheritance alternative.
Am I misguided in my efforts?


Not enough information to say, but probably not; however, it seems
you come from a Java or perhaps C# background, and need to ditch
much of the low-level details from the previous language, while
holding on the high-level concepts (which you'll have to _implement_,
some times by adhering strictly to some convention, in C++).

Jul 19 '05 #2
Kevin L wrote:
Below is a code snippet that fails to compile under vc.net

class BaseInterface
{
public:
virtual void f() = 0;
};

class BaseImplementat ion : public BaseInterface
{
public:
void f(){}
};

class ExtraInterface : public BaseInterface
{
virtual void g() = 0;
};

class ExtraImplementa tion: public BaseImplementat ion, public
ExtraInterface
{
void g(){}
};

void func()
{
ExtraImplementa tion d;
}

The compiler complains that ExtraImplementa tion is an abstract class
and therefore cannot be initialized. Function f, despite being
defined in BaseImplementat ion, is undefined in ExtraImplementa tion.

I hope it's clear what I'm trying to do, i.e., maintaining two
inheritance lines that parallel each other. One line consists of
interfaces and the other implementation. Am I misguided in my
efforts?


The virtual inheritance mechanism is what is intended to solve the problem.

class BaseInterface
{
public:
virtual void f() = 0;
};

class BaseImplementat ion : public *virtual* BaseInterface
{
public:
void f(){}
};

class ExtraInterface : public *virtual* BaseInterface
{
virtual void g() = 0;
};

class ExtraImplementa tion: public BaseImplementat ion, public
ExtraInterface
{
void g(){}
};

void func()
{
ExtraImplementa tion d;
}

Jul 19 '05 #3
al***@start.no (Alf P. Steinbach) wrote in message news:<3f******* *********@News. CIS.DFN.DE>...
On 22 Sep 2003 17:39:26 -0700, in*****@hotmail .com (Kevin L) wrote:
Below is a code snippet that fails to compile under vc.net

class BaseInterface
{
public:
virtual void f() = 0;
};

class BaseImplementat ion : public BaseInterface
{
public:
void f(){}
};

class ExtraInterface : public BaseInterface
{
-- public:

virtual void g() = 0;
};

class ExtraImplementa tion: public BaseImplementat ion, public
ExtraInterfa ce
{


-- public:

void g(){}
};

void func()
{
ExtraImplementa tion d;
}

....
Instead of using virtual inheritance (the tech level solution) I
recommend _factoring out_ the ExtraInterface, like this:
class ExtraInterface
{
public:
virtual void g() = 0;
};

....
Ah, but now I can't pass an object of ExtraImplementa tion as
ExtraInterface and have the BaseInterface functions available without
casting.

Now if I stick to strict abstract classes for the interfaces (no data
members), I can sidestep much of the messiness involved in virtual
inheritance, correct? The inheritance tree I am building is meant for
exception classes. I expect them to be constructed and used quite
infrequently. Right now I'm leaning toward the virtual inheritance
solution. Any reason not to?

Thanks for the reply.

Kevin
Jul 19 '05 #4
On 23 Sep 2003 00:58:28 -0700, in*****@hotmail .com (Kevin L) wrote:
al***@start. no (Alf P. Steinbach) wrote in message news:<3f******* *********@News. CIS.DFN.DE>...
On 22 Sep 2003 17:39:26 -0700, in*****@hotmail .com (Kevin L) wrote:
>Below is a code snippet that fails to compile under vc.net
>
>class BaseInterface
>{
>public:
> virtual void f() = 0;
>};
>
>class BaseImplementat ion : public BaseInterface
>{
>public:
> void f(){}
>};
>
>class ExtraInterface : public BaseInterface
>{
-- public:

> virtual void g() = 0;
>};
>
>class ExtraImplementa tion: public BaseImplementat ion, public
>ExtraInterfa ce
>{


-- public:

> void g(){}
>};
>
>void func()
>{
> ExtraImplementa tion d;
>}
>

...

Instead of using virtual inheritance (the tech level solution) I
recommend _factoring out_ the ExtraInterface, like this:
class ExtraInterface
{
public:
virtual void g() = 0;
};

...
Ah, but now I can't pass an object of ExtraImplementa tion as
ExtraInterfa ce and have the BaseInterface functions available without
casting.


First, you _can_ hardwire the relationship, via an accessor method
in ExtraInterface.

But, second, why would you want to do that?
Now if I stick to strict abstract classes for the interfaces (no data
members), I can sidestep much of the messiness involved in virtual
inheritance, correct? The inheritance tree I am building is meant for
exception classes. I expect them to be constructed and used quite
infrequently . Right now I'm leaning toward the virtual inheritance
solution. Any reason not to?


1 In the most derived class you may have to call the constructor of
the root class.

2 Virtual inheritance mixed with non-virtual inheritance is very
messy. You're in for a maintainance nightmare.

3 It's a tad inefficient, also.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8188
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by making my contnent dlls implement an interface created in vb6. The viewer application is bound to this interface. This way, I am able to add Content without redeploying the dlls (I just have to add the new dlls). I want to write new content for...
13
3246
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
60
4903
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
15
2785
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs. For example: f(3) f(3, )
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.