Wierd Inheritance problem

B

brentritchie

Hello everyone,

I have this problem, I want to declare an interface class and have
it inherit from other interfaces. But when I define a concrete class I
want to substitute implementations of interfaces instead of the
interfaces themselves; In much the same way I can use a refrence to an
interface within a class but point to any object that implements that
interface.To illustrate:

class AInterface
{
public:
virtual char* something()=0;
}

class AImplementation
{
public:
char* something(){return "doing something...";};
}

class BInterface : public AInterface
{

}

Now the problem is, how do I inherit from BInterface but use
AImplementation instead of AInterface? I looked at late binding and
runtime inheritence but they don't seem to be what I want. Any pointers
to what I want to do?

The reason I don't want to just use concrete classes is that I don't
want to build a new set of classes every time I change platforms or
other external parts of my program. I'll just have to do
implementation\platform specific code on a few interfaces instead.

Thanks to all that apply.

p.s. I also posted this to comp.std.c++ but I did not cross post
because I don't think it's polite to cross post to a moderated and
unmoderated group.
 
A

Alf P. Steinbach

* (e-mail address removed):
class AInterface
{
public:
virtual char* something()=0;
}

class AImplementation
{
public:
char* something(){return "doing something...";};
}

class BInterface : public AInterface
{

}

Now the problem is, how do I inherit from BInterface but use
AImplementation instead of AInterface?

You can either implement the BInterface functions and forward to an instance
of AImplementation (with the code you've presented that's the only option
because AImplementation does not implement AInterface, but then, the lacking
semicolons make me suspect it's not more than a "kind-of-like" example), or
you can use virtual inheritance of interfaces:

class AInterface
{
public:
virtual char const* something() const=0;
};

class AImplementation: public virtual AInterface
{
public:
virtual char const* something() const{return "doing something...";};
};

class BInterface : public virtual AInterface
{
public:
virtual void foo() {}
};

class Client: public AImplementation, public virtual BInterface
{
};
 
B

Brent Ritchie

Thanks,

The code you presented here is actually what I meant to write.
Thanks for reading between the lines. I tested what you gave me here
and this is exactly the behaviour I was looking for. So, now I'm going
to look through my books and learn about virtual inheritance. I've
never even heard of virtual inheritance before so I guess i'm starting
to use slightly advanced features. Anyways thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top