By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Wierd Inheritance problem

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* br**********@personainternet.com:

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
{
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.