Answer the question
In order to leave comments, you need to log in
Is it possible to do without virtual inheritance?
I have diamond inheritance of interfaces: the common interface IReadableDataset is virtually inherited by two others: IEditableDataset and IStatisticalDataset . In turn, there is an implementation of CDatasetImpl , which implements IStatisticalDataset and IEditableDataset .
Is it possible to do without virtual inheritance? Like some clever templating trick?
class IReadableDataset
{
public:
virtual void SayReadableDataset() const = 0;
};
class IEditableDataset: virtual public IReadableDataset
{
public:
virtual void SayEditableDataset() const = 0;
};
class IStatisticalDataset: virtual public IReadableDataset
{
public:
virtual void SayStatisticalDataset() const = 0;
}
class CDatasetImpl: public IEditableDataset, public IStatisticalDataset
{
public:
virtual void SayReadableDataset() const;
virtual void SayEditableDataset() const;
virtual void SayStatisticalDataset() const;
};
Answer the question
In order to leave comments, you need to log in
If classes whose names begin with "I" are interfaces, that is, they do not have their own data and logic, then such multiple inheritance is not considered bad manners.
But perhaps in your case you can rename IEditableDataset to IWritableDataset and not inherit it from IReadableDataSet.
This is a classic case of multiple inheritance of interfaces. Because they are stateless, you just need to remove the virtual keyword.
By the way, why did you need virtual inheritance here?
equivalent on templates, for example, like this, but not very beautiful ...
class ReadableDataset
{
public:
virtual void SayReadableDataset() const = 0;
};
template<class Base>
class EditableDataset: public Base
{
public:
virtual void SayEditableDataset() const = 0;
};
template<class Base>
class StatisticalDataset: public Base
{
public:
virtual void SayStatisticalDataset() const = 0;
};
class CDatasetImpl: public StatisticalDataset<EditableDataset<ReadableDataset> >
{
public:
virtual void SayReadableDataset() const
{
std::cout << "SayReadableDataset" << std::endl;
};
virtual void SayEditableDataset() const
{
std::cout << "SayEditableDataset" << std::endl;
};
virtual void SayStatisticalDataset() const
{
std::cout << "SayStatisticalDataset" << std::endl;
};
};
template<class T>
void statistic(const StatisticalDataset<T>& x)
{
x.SayReadableDataset();
x.SayStatisticalDataset();
}
template<class T>
void edit(EditableDataset<T>& x)
{
x.SayReadableDataset();
x.SayEditableDataset();
}
void read(const ReadableDataset& x)
{
x.SayReadableDataset();
}
int main()
{
CDatasetImpl a;
read(a);
edit(a);
statistic(a);
return 0;
}
No.
It's like trying not to use a keyword virtual
to indicate that a given method is virtual.
Those. you have all the conditions for using virtual inheritance - and this is not a crutch - without its use - this is a crutch.
If you don't like virtual inheritance, rethink your class hierarchy.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question