Re: derived class and virtual function
On Aug 18, 1:21 am, "Alf P. Steinbach /Usenet" <alf.p.steinbach
+use...@gmail.com> wrote:
* =D6=F6 Tiib, on 18.08.2010 02:13:
On 18 aug, 02:54, red floyd<no.spam.h...@its.invalid> wrote:
On 8/17/2010 1:54 AM, James Kanze wrote:
On Aug 13, 10:09 am, "Francesco S. Carta"<entul...@gmail.com> wrot=
e:
#include<iostream>
using namespace std;
class Base {
public:
Base(int data = 0) : data(data) {};
Base(const Base& base) : data(base.data) {};
virtual Base* Clone() const {
cout<< "cloning Base"<< endl;
return new Base(*this);
}
int Data() const {
return data;
}
private:
int data;
};
class Derived : private Base {
public:
Derived(int data = 0) : Base(data) {};
Derived(const Derived& derived) : Base(derived) {};
Base* Clone() const {
Here, you're still returning a Base. Any conversion takes place
in Derived (where the derivation is visible).
Try changing the return type to Derived*; the compiler should
complain then.
See =A710.3/5 in the standard for details.
James, I thought that the compiler should *not* complain if the
return type of Derived::Clone was Derived*. Isn't that a
covariant return type?
Did you eyeball =A710.3/5? Base is not accessible base class
of Derived so it is not a covariant there by standard.
Base is accessible /in/ D.
But not generally.
That makes the function covariant per =A710.3/5.
=A710.3/5 doesn't say "accessible in D" (at least not in C++03);
it just says "accessible". Without saying where, but since the
original language said "accessible in D", and this was changed,
we have to assume that it means "generally accessible", or
something like that. Which is, of course, the most logical
interpretation, because the actual conversion takes place after
the return, outside of D.
On the other hand, the example at the end of =A710.3/5 does
suggest that accessibility is checked in the context of D.
(Examples are non-normative, but they do indicate intent.) So
maybe the question should be raised in comp.std.c++.
--
James Kanze