T
tron.thomas
Please consider the following code:
class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};
class Concrete : public virtual Abstract
{
private:
virtual void Method() {}
};
void UseAbstract()
{
Concrete concrete;
Abstract& abstract = concrete;
abstract.Method();
}
The implementation for Abstract::Method provided in the Concrete class
is private even though the declaration of the method is public in the
derived base class. Yet, compiling the code on three different
compilers produces no errors or warnings. I think many developer might
find this surprising and puzzling. I, myself, am not sure what to
think of it.
The end effect is that the implementation for the Abstract class works
as expected. An instance of Concrete can be used as an Abstract
instance and it works as expected with respect to the Abstract
interface. Method is public for Abstract and can be accessed just
fine, even though it is private with respect to its Concrete
implementation.
However, it seems that there is something wrong, or at least worrisome
about the fact that this can be done.
What can people say about the correctness of this code?
If it is correct, what are the reasons classes are allow to behave this
way?
class Abstract
{
public:
virtual ~Abstract() {}
virtual void Method() = 0;
};
class Concrete : public virtual Abstract
{
private:
virtual void Method() {}
};
void UseAbstract()
{
Concrete concrete;
Abstract& abstract = concrete;
abstract.Method();
}
The implementation for Abstract::Method provided in the Concrete class
is private even though the declaration of the method is public in the
derived base class. Yet, compiling the code on three different
compilers produces no errors or warnings. I think many developer might
find this surprising and puzzling. I, myself, am not sure what to
think of it.
The end effect is that the implementation for the Abstract class works
as expected. An instance of Concrete can be used as an Abstract
instance and it works as expected with respect to the Abstract
interface. Method is public for Abstract and can be accessed just
fine, even though it is private with respect to its Concrete
implementation.
However, it seems that there is something wrong, or at least worrisome
about the fact that this can be done.
What can people say about the correctness of this code?
If it is correct, what are the reasons classes are allow to behave this
way?