473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

extending an interface

Hi,

I'm looking for a more concise way than below to extend an existing
interface.
Just imagine that class Interface would have 1000 other members and class
DerivedInterface only needs to add / modify _one_ member. The implementation
shall remain invisible to the application (i.e. main()).

Thanks for any suggestions,

Christof

#include <stdio.h>

class Interface {
public:
static Interface *create(void);
virtual void a(void) = 0;
virtual void b(void) = 0;
// and 1000 other pure virtual functions
};

class DerivedInterface: public Interface {
public:
DerivedInterface(void):
_base(Interface::create()) {
}
void a(void) {
printf("A\n");
}
void b(void) {
_base->b();
}
// and the 1000 other, unmodified functions
private:
Interface *_base;
};

int main(void) {
Interface *base = Interface::create();
base->a();
base->b();
Interface *derivedInterface = new DerivedInterface();
derivedInterface->a();
derivedInterface->b();
}

// everything below shall be kept invisible to the compilation unit

class Implementation: public Interface {
public:
void a(void) {
printf("a\n");
}
void b(void) {
printf("b\n");
}
};

Interface *Interface::create(void) {
return new Implementation;
}
Jul 23 '05 #1
3 3218
Christof Warlich wrote:
Hi,

I'm looking for a more concise way than below to extend an existing
interface.
Just imagine that class Interface would have 1000 other members and class
DerivedInterface only needs to add / modify _one_ member. The implementation
shall remain invisible to the application (i.e. main()).

Thanks for any suggestions,
If you only want to override certain member functions, you'll have to
use inheritance. The base will have to implement its virtual (non pure)
member functions to provide a "default" behavior. Derived classes will
be free to either 1) change the behavior by overriding the virtual
functions or 2) let the base class do the job.

Now if you don't inherit from a base class which provide an
implementation (as you do), it's impossible.
Christof

#include <stdio.h>

class Interface {
public:
static Interface *create(void);
virtual void a(void) = 0;
virtual void b(void) = 0;
// and 1000 other pure virtual functions
};

class DerivedInterface: public Interface {
public:
DerivedInterface(void):
_base(Interface::create()) {
}
void a(void) {
printf("A\n");
}
void b(void) {
_base->b();
}
// and the 1000 other, unmodified functions
private:
Interface *_base;
};


I don't know about your design much, but you could have Inteface do
DerivedInterface's job:

class Interface {
public:

Interface();

virtual void a()
{
impl_->a();
}

virtual void b()
{
impl_->b();
}

// and 1000 other pure virtual functions

private:
Interface *impl_;
};

class DerivedInterface: public Interface {
public:
DerivedInterface()
{
}

void a()
{
printf("A\n");
}
};

class Implementation: public Interface {
public:
void a()
{
printf("a\n");
}

void b()
{
printf("b\n");
}
};

Interface::Interface()
: impl_(new Implementation)
{
}

That's pretty much the Pimpl pattern. It changes the design, but I
think it's the only way to achieve your goal.
Jonathan

Jul 23 '05 #2
Christof Warlich wrote:
I'm looking for a more concise way than below to extend an existing
interface.
What does it mean (in your POV) "to extend an existing interface"?
I understand it as "define another interface with more functions".
Just imagine that class Interface would have 1000 other members and class
DerivedInterface only needs to add / modify _one_ member.
So, is it "add" or "modify"?
The
implementation
shall remain invisible to the application (i.e. main()).
What does that mean?
Thanks for any suggestions,

Christof

#include <stdio.h>

class Interface {
public:
static Interface *create(void);
virtual void a(void) = 0;
virtual void b(void) = 0;
// and 1000 other pure virtual functions
};

class DerivedInterface: public Interface {
If you derive publicly to establish conversions (the "is-a" relationship),
then you have to deal with the consequences (like the need to define all
pure functions to make this class concrete). Such are the rules.
public:
DerivedInterface(void):
_base(Interface::create()) {
}
void a(void) {
printf("A\n");
}
void b(void) {
_base->b();
}
// and the 1000 other, unmodified functions
What do you mean by "unmodified"? They are pure, aren't they? So, you
need to define them _all_ here.
private:
Interface *_base;
};

int main(void) {
Interface *base = Interface::create();
base->a();
base->b();
Interface *derivedInterface = new DerivedInterface();
derivedInterface->a();
derivedInterface->b();
}

// everything below shall be kept invisible to the compilation unit

class Implementation: public Interface {
public:
void a(void) {
printf("a\n");
}
void b(void) {
printf("b\n");
}
};

Interface *Interface::create(void) {
return new Implementation;
}


You can't expect your 'DerivedInterface' class to suddenly become concrete
(instantiable) without defining all pure virtual functions in 'Interface'.
What you should be looking at is another way to create an 'Interface*' by
some factory method without redefining the whole bag of virtual functions.
IOW, you need to create a 'DerivedImplementation' as well as the
'DerivedInterface':

class ABC {
public:
static ABC* create();
static void destroy(ABC*);

virtual ~ABC() {}
virtual void a() = 0;
virtual void b() = 0;
};

class ABC2 : public virtual ABC {
public:
static ABC2* create();
static void destroy(ABC2*);

virtual void c() = 0;
};

int main() {
ABC* abc = ABC::create();
abc->a();
abc->b();
ABC::destroy(abc);

ABC2* abc2 = ABC2::create();
abc2->a();
abc2->b();
abc2->c();
ABC2::destroy(abc2);
}
-------------------------------------------- elsewhere
#include <iostream>
class ABC_impl : public virtual ABC {
public:
void a() { std::cout << "ABC_impl::a\n"; }
void b() { std::cout << "ABC_impl::b\n"; }
};

#include <iostream>
class ABC2_impl : public ABC2, public ABC_impl {
public:
void c() { std::cout << "ABC2_impl::c\n"; }
};

ABC* ABC::create() {
return new ABC_impl;
}

void ABC::destroy(ABC* abc) {
delete abc;
}

ABC2* ABC2::create() {
return new ABC2_impl;
}

void ABC2::destroy(ABC2* abc2) {
delete abc2;
}
V
Jul 23 '05 #3
Hi Jonathan, Victor,

thanks a lot for the comprehensive help. My goal was to strictly
separate the interface
from the implementation, which works fine as long as inheritance in not
needed, while
it becomes somewhat ugly when implementation of a derived class just
with the knowledge
of the interface is necessary.

Cheers,

Christof
Jul 23 '05 #4

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

Similar topics

3
by: ch424 | last post by:
Hi there, I'm using Python 2.4.1 on Ubuntu Linux, and I'm having problems extending python in C: The C code is below: #include <Python.h> #include "ni488.h"
1
by: Matthias Kaeppler | last post by:
Sorry if this has been discussed before (I'm almost certain it has), but I didn't know what to google for. My problem is, I have a class, a gtkmm widget, and I want it to serve as a base class...
4
by: Matt | last post by:
Hi, I've been thinking about how to do this, but can't think of a solution. I have a class that is derived from System.Web.UI.WebControls.DataGrid which works a treat, but I'd like to extend...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
1
by: Scott | last post by:
Hi, Has anyone seen this article in MSDN Magazine? http://msdn.microsoft.com/msdnmag/issues/04/08/GeneticAlgorithms/default.aspx My GA twiddlings puffed out years ago, but the idea of using...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
13
by: interec | last post by:
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(); } //...
8
by: Floortje | last post by:
Hi i have been struggeling with this question for quite some time now. I have some helper classes that handle images (upload an image, create thumbnails and show a imagelist), links (add link,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.