I don't think that's what is meant (although it's hard to
say---the sentence is unclear, at least without its surrounding
^^^^^^^^^^^
^^^^^^^
[...]
Here I write down the complete paragraph; perhaps that will be useful
in explaining the last sentence.
In Stanley Lippman's 'C++ Primer Fourth Edition', in page 564, the
following is mentioned:
"With one exception, the declaration of a virtual function in the
derived class must exactly match the way the function is defined in
the base. That exception applies to virtuals that return a
reference(or pointer) to a type that is itself a base class. A virtual
function in the derived class can return a reference or pointer to a
class that is PUBLICLY derived from the type returned by the base
class function."
I have written the follwing program a.cpp after going through one of
Stuart Redmann's replies in this thread:
#include <cstdlib>
#include <iostream>
using namespace std;
class X
{
public:
void print(void) const;
};
inline void X:
rint(void) const
{
cout << "from X:
rint() function" << endl;
return;
}
class Y : public X
{
public:
void print(void) const;
};
inline void Y:
rint(void) const
{
cout << "from Y:
rint() function" << endl;
return;
}
class Base
{
public:
virtual X* get(X& x) const;
virtual ~Base();
};
inline X* Base::get(X& x) const
{
cout << "Base::get() called" << endl;
return &x;
}
inline Base::~Base()
{
}
class Derived : public Base
{
public:
virtual Y* get(Y& y) const;
virtual ~Derived();
};
inline Y* Derived::get(Y& y) const
{
cout << "Derived::get() called" << endl;
return &y;
}
inline Derived::~Derived()
{
}
int main()
{
X x;
Y y;
Derived* pd = new Derived();
Y* py = pd->get(y);
py->print();
delete pd;
pd = 0;
Base* pb = new Base();
X* px = pb->get(x);
px->print();
delete pb;
pb = 0;
return EXIT_SUCCESS;
}
This program compiles fine with g++ and produces the output:
Derived::get() called
from Y:
rint() function
Base::get() called
from X:
rint() function
Does the above program correctly demonstrate what is mentioned in the
paragraph(in Stanley Lippman's book - page 564) that I had mentioned
completely above? Kindly reply. If I am wrong in understanding that
paragraph, please explain it.
Thanks
V.Subramanian