Multiple inheritance question

T

t-tocs

In C++, is it possible to design a class so that it will not allow
multiple inheritance?
 
C

Christopher

In C++, is it possible to design a class so that it will not allow
multiple inheritance?

That doesn't make sense. How can you design something to not allow you
to design it? If you design the class, you decide what it inherits...

class Derived : public Base
{
};

Derived inherits Base.

You probably meant, is it possible to design a class that will not
allow any derived class to inherit from anything but itself?
Probably. But, why would you want to?
 
T

t-tocs

No, I mean a base class, Base1, so that it will not allow the
following:

class Derive: public Base1, public Base2 {

}

but will allow:

class Derive: public Base1 {

}
 
C

Christopher

No, I mean a base class, Base1, so that it will not allow the
following:

class Derive: public Base1, public Base2 {

}

but will allow:

class Derive: public Base1 {

}

That's exactly what I said:


Not without extreme hack like coding. Again, why would you want to? It
is extremely rude to limit the client of your class in a way that
would defy standard expectations. If you have some design that
requires it, there is probably something wrong with the design.
 
J

jason.cipriani

It is not for a particular reason.
I was just wondering if it was possible.

What made you think of this?

To let you prevent that, the compiler would have to go through the
base class list and if there was more than one base but one of the
base classes was marked with a hypothetical "no multiple inheritance"
property, fail to compile the code. C++ does not have that feature,
AFAIK.

If you are in a situation where having a class inherit from your class
and from something else would be a problem, the best solution is, of
course, to rethink your design. If you can't avoid it, the only thing
you can really do is clearly document the restriction and say "you
have been warned".

Jason
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top