473,480 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Interfaces and functions...

Lets say we have some interfaces declared:

class A { virtual void a() = 0; };
class B { virtual void b() = 0; };
class C { virtual void c() = 0; };

We also have some classes implementing none or some of these
interfaces. Now I think it would be neat if I could define a function
that will accept an object that implemts both interface A and B as
input. Something like this:

void AcceptAAndB(/* parameter type */);

This funtion should accept the following classes:

class AB : public A, public B { };
class ABC : public A, public B, public C { };

The function should not accepts arguments of the following classes:

class JustA : public A { };
class JustB : public B { };

As far as I can see there is no easy way of doing this in C++. But I
would really like to be able to do it. =)

Has anyone around here been trying to do something like this? Any
ideas or thoughts along these lines?

Regards,
Mattias
Jul 22 '05 #1
4 1166

"Mattias Brändström" <"thebrasse at brasse dot org"> wrote in message
news:41*********************@news.luth.se...
Lets say we have some interfaces declared:

class A { virtual void a() = 0; };
class B { virtual void b() = 0; };
class C { virtual void c() = 0; };

We also have some classes implementing none or some of these
interfaces. Now I think it would be neat if I could define a function
that will accept an object that implemts both interface A and B as
input. Something like this:

void AcceptAAndB(/* parameter type */);

This funtion should accept the following classes:

class AB : public A, public B { };
class ABC : public A, public B, public C { };

The function should not accepts arguments of the following classes:

class JustA : public A { };
class JustB : public B { };

As far as I can see there is no easy way of doing this in C++. But I
would really like to be able to do it. =)

Has anyone around here been trying to do something like this? Any
ideas or thoughts along these lines?

Regards,
Mattias


I think you should consider generic programming instead of object orientated
programming

template <class T>
void AcceptAAndB(T& t)
{
t.a();
t.b();
}

This will fail to compile if type T (whatever it is) does not have zero
argument methods called a and b.

john

Jul 22 '05 #2
Mattias Brändström wrote:
Lets say we have some interfaces declared:

class A { virtual void a() = 0; };
class B { virtual void b() = 0; };
class C { virtual void c() = 0; };

We also have some classes implementing none or some of these
interfaces. Now I think it would be neat if I could define a function
that will accept an object that implemts both interface A and B as
input. Something like this:

void AcceptAAndB(/* parameter type */);

This funtion should accept the following classes:

class AB : public A, public B { };
class ABC : public A, public B, public C { };

The function should not accepts arguments of the following classes:

class JustA : public A { };
class JustB : public B { };

As far as I can see there is no easy way of doing this in C++. But I
would really like to be able to do it. =)

Has anyone around here been trying to do something like this? Any
ideas or thoughts along these lines?


Here is an idea: KISS ['keep it simple, silly']. Accepting interfaces
is pointless unless you use the functions they implement. So, if you
simply define the 'AcceptAAndB' as a template that uses both 'a' and 'b'
functions, you've achieved the goal, as far as C++ is concerned:

template<class Interface> void AcceptAAndB(Interface& ab) {
...
ab.a();
...
ab.b();
}

because a class that doesn't implement both 'a' and 'b' will be rejected
at compile-time. (Of course this presumes 'a' and 'b' are callable,
unlike in your example, where they are private)

Some will argue that there can exist another class

class Implements_a_and_b_not_related_to_A_or_B {
public:
int a();
double b();
};

which the function would happily accept, but this is where you have to
decide whether you need the run-time polymorphism or just compile-time
one.

Another way of doing that is, of course, to introduce a class AB:

class AB : public A, public B {
public:
void a() = 0;
void b() = 0;
};

from which all other classes should derive to be "accepted":

void AcceptAAndB(AB& ab) {
...
}

.. There are probably some template tricks you can play to limit the set
of types a template function can accept. Something like "isBaseOrDerived"
or something in that area. They are neat tricks and demonstrate the power
of C++ generic programming abilities, but I am still to encounter real-
life need for those (not to say that there isn't any).

V
Jul 22 '05 #3
On Mon, 01 Nov 2004 15:52:21 +0100, Mattias Brändström <"thebrasse at
brasse dot org"> wrote:
Lets say we have some interfaces declared:

class A { virtual void a() = 0; };
class B { virtual void b() = 0; };
class C { virtual void c() = 0; };

We also have some classes implementing none or some of these
interfaces. Now I think it would be neat if I could define a function
that will accept an object that implemts both interface A and B as
input. Something like this:

void AcceptAAndB(/* parameter type */);

This funtion should accept the following classes:

class AB : public A, public B { };
class ABC : public A, public B, public C { };

The function should not accepts arguments of the following classes:

class JustA : public A { };
class JustB : public B { };

As far as I can see there is no easy way of doing this in C++. But I
would really like to be able to do it. =)

Has anyone around here been trying to do something like this? Any
ideas or thoughts along these lines?
You can do something like this:

#include "boost/mpl/and.hpp"
#include "boost/type_traits.hpp"
#include "boost/enable_if.hpp"

void AcceptAAndBImpl(A& viewAsA, B& viewAsB); //implement in .cpp file

using namespace boost;

template <class T>
enable_if<
mpl::and_<
is_base_and_derived<A, T>,
is_base_and_derived<B, T>,,

void>::type AcceptAAndB(T& t)
{
AcceptAAndBImpl(t, t); //should always compile without error
}

Apologies for any errors. The above should interact fine with
overloading. See enable_if, mpl and type_traits libraries at
www.boost.org. Obviously, the AcceptAAndBImpl bit is just there to
allow you to implement the function in a .cpp file by putting the
implementation in a non-template function.

Tom
Jul 22 '05 #4
I will just supply another possibility (or maybe mine is overlapping some of
the others..)

Inherit from the InterfaceSupplier and "ask it" what interfaces it
implements, via the getInterface functions...

class A;
class B;
class C;
class InterfaceSupplier {
public:
virtual A* getInterfaceA(){ return 0; };
virtual B* getInterfaceB(){ return 0; };
virtual C* getInterfaceC(){ return 0; };
virtual ~InterfaceSupplier();
};

class A {
public:
virtual void a()=0;
};

class B {
public:
virtual void b()=0;
};

class ConcreteA : public A {
public:
virtual void a(){};
};

class ConcreteB : public B {
public:
virtual void b(){};
};

The one i would prefer...

class AB : public InterfaceSupplier {
public:
virtual A* getInterfaceA(){ return &a_; };
virtual B* getInterfaceB(){ return &b_; };
private:
ConcreteA a_;
ConcreteB b_;
};

OR...

class AB : public InterfaceSupplier,class A,class B {
public:
virtual A* getInterfaceA(){ return this; };
virtual B* getInterfaceB(){ return this; };
virtual void a(){};
virtual void b(){};
};

Jesper

"Mattias Brändström" <"thebrasse at brasse dot org"> wrote in message
news:41*********************@news.luth.se...
Lets say we have some interfaces declared:

class A { virtual void a() = 0; };
class B { virtual void b() = 0; };
class C { virtual void c() = 0; };

We also have some classes implementing none or some of these
interfaces. Now I think it would be neat if I could define a function
that will accept an object that implemts both interface A and B as
input. Something like this:

void AcceptAAndB(/* parameter type */);

This funtion should accept the following classes:

class AB : public A, public B { };
class ABC : public A, public B, public C { };

The function should not accepts arguments of the following classes:

class JustA : public A { };
class JustB : public B { };

As far as I can see there is no easy way of doing this in C++. But I
would really like to be able to do it. =)

Has anyone around here been trying to do something like this? Any
ideas or thoughts along these lines?

Regards,
Mattias

Jul 22 '05 #5

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

Similar topics

17
2328
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
30
2465
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
13
1835
by: rajkumar | last post by:
Hey all Here is my question Consider two classes Class A : SomeSystemClassA Class B : SomeSystemClassB Now I have to add a common interface to both class A and class B. The
42
2441
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read...
9
2532
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
18
1958
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
2051
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
40
2683
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I'm really annoyed at Python - and not for the reasons already mentioned on this list. Everyone know that programming is supposed to be a dark art, nearly impossible to learn. Computer code is...
18
1744
by: abcd | last post by:
Can someone tell me whats the exact advantage of Interfaces in C#. To me abstract classes serves the purpose and have all good things then what is special about interfaces...can someone tell me...
0
7037
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,...
0
7034
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
7076
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...
1
6732
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...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4768
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...
0
4472
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...
0
2990
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.