Re: Inheritance: Implementing pure virtual functions by inheriting
from an object providing an implementation
On Mar 31, 11:27 am, Jaco Naude <naude.j...@gmail.com> wrote:
On Mar 31, 10:57 am, "Alf P. Steinbach" <al...@start.no> wrote:
* Jaco Naude:
Hi
I'm wondering if an idea that I have should work. Well the compiler
(mingw) does not seem to allow it, but I'm not sure why it does not
work.
I have an interface class:
class IManagable {
public:
virtual void setID(int new_id) = 0;
virtual int getID() = 0;
}
And an object implementing this interface:
class ManagableObject {
public:
ManagableObject () { id = -1; }
~ManagableObject ();
// Implement functions
inline void setID(int new_id) { id = new_id; }
inline int getID() { return id; }
private:
int id;
}
I've read in the FAQ that inheritance should not be used for code
reuse in general. But in my case I think it is a good idea. I want to
be able to assign unique IDs to objects implementing the interface. I=
f
all of these objects can use the interface as well as the
ManagableObject implementation of the interface, it will reduce my
workload quite a lot I believe.
Now the problem. I have an object that I want to be able to manage
that looks like this:
class ManageMe : virtual public IManagable, public ManagableObject {
ManageMe () {}
~ManageMe () {}
}
This complains that the IManagable pure virtual public functions
aren't implemented by ManageMe. So my approach to accomplish this is
not working. Is there a different way that I can do this without
having to implement the IManagable interface in ManageMe (where the
implementation calls the ManagableObject functions).
Thanks in advance for any advice
class ManagableObject: public virtual IManagable
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url:http://alfps.izfree.c=
om/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Link=
ing
to it is even better! Thanks in advance!
Hi Alf
Thanks for the suggestion, I have not thought of that.
I'll give it a go.
Thanks
Jaco
Hi again,
I've tried the new implementation but casting becomes really confusing
in this implementation. Let me give some details on my implementation.
The ManageMe class from my first post looks like this:
class ManageMe: public GCF::AbstractComponent, public ManagableObject,
virtual public IToolbox, virtual public IData
where each of the parent classes are defined as follows (Using Qt,
hence the reason for the QObject class below):
class AbstractComponent : public QObject -- This is a singleton
class ManagableObject : virtual public IManagable
class IData : virtual public IContainer
class IToolbox : virtual public IContainer
class IManagable : virtual public IContainer
The IContainer class is a container class defined as follows:
class IContainer
{
public:
virtual QObject* containerObject() = 0;
};
In the ManageMe class the IContainer interface is implemented as
follows:
QObject* ManageMe ::containerObject()
{
return this;
}
Ok, that's my class hierarchy explained. Now I'm trying to cast to
IManagable* when I have a QObject pointer. The way I try to do it is
shown below and causes a program crash:
if (qobject_ptr) {
ManagableObject* tmp_ref = dynamic_cast<ManagableObject*>
(qobject_ptr);
if (tmp_ref) {
IManagable* managable_ptr = dynamic_cast<IManagable*>
(tmp_ref);
if (managable_ptr) {
// Access managable_ptr's contents here causes crash.
}
}
}
I've tried various variations of the above without anything working.
How is it possible to do this? Is it possible at all?
Thank you
Jaco