Classes in a hierarchy inheriting from the same class

J

Joseph Paterson

Hi all,

This has popped up in a project that I'm working on, and I was
wondering whether it is ok/not ok, and what really happens in the
following case.
I have a base class for most of my classes, called IObject. It
basically has grab()/drop() methods, which increment/decrement a
reference counter. drop() calls delete this if the reference counter
reaches 0. They also have a string object, which holds a debug name:

class IObject
{
public:
IObject() : reference_counter(1) {}
void grab();
void drop();
string getDebugName();
};

then I have two classes, say IA (interface to A), and A. I was doing
the following, and got no mistakes:
class IA : public IObject
{
};

class A : public IA, public IObject
{
};

When I create an object of type A, what goes on exactly? Does it
inherit from two IObject instances, and does it have two reference
counters?I put a printf statement in the constructor of IObject, and
it seems that it actually just gets called once, but is that right?

Thanks in advance for any guidance on this,

Joseph Paterson.
 
A

Alf P. Steinbach

* Joseph Paterson:
Hi all,

This has popped up in a project that I'm working on, and I was
wondering whether it is ok/not ok, and what really happens in the
following case.
I have a base class for most of my classes, called IObject.

Happily the Universal Base Class disappeared sometimes in the mid 1990's.

But now it rears its ugly head again.

It
basically has grab()/drop() methods, which increment/decrement a
reference counter. drop() calls delete this if the reference counter
reaches 0.

What's wrong with boost::intrusive_ptr?

They also have a string object, which holds a debug name:

class IObject
{
public:
IObject() : reference_counter(1) {}
void grab();
void drop();
string getDebugName();
};

If this is really an interface, the member functions should be virtual.

But I think it's just misnamed.

I.e., that it's really a class Object.

then I have two classes, say IA (interface to A), and A. I was doing
the following, and got no mistakes:
class IA : public IObject
{
};

class A : public IA, public IObject
{
};

When I create an object of type A, what goes on exactly? Does it
inherit from two IObject instances,
Yes.


and does it have two reference
counters?
Yes.


I put a printf statement in the constructor of IObject, and
it seems that it actually just gets called once, but is that right?

No.

For interface use virtual inheritance.
 
T

terminator

When I create an object of type A, what goes on exactly? Does it
inherit from two IObject instances, and does it have two reference
counters?I put a printf statement in the constructor of IObject, and
it seems that it actually just gets called once,


*A surprising output!!!!!* are you certain that it prints just once???
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top