Re: Inheritance based type conversion and private inheritance
"Daniel Kr?gler" <daniel.kruegler@googlemail.com> schrieb im Newsbeitrag
news:ip0v9p$hi7$1@dont-email.me...
class Base
{
// Required for polymorphic deletion,
// though this is just a side issue.
virtual ~Base() {}
};
Are you sure that this is the way how you wrote your code? Note that the
destructor of base is private here, so there is no chance to delete a
pointer to Base or to Derived within the SmartPtr. In the following, I
assume that Base::~Base() was actually declared as public, else you could
not compile anything of your code.
You are right, that is a mistake of mine. The code does not compile with a
private destructor, I just did not notice because the code did not compile
anyway because of the error in the first line of main().
The compiler is defect and should not accept your very last
initialization.
That's good news because I was already beginning to doubt my sanity. I don't
know whether this is a known issue, maybe I should file a report with
Microsoft.
I assume you are aware that above definition of a smart-pointer does not
ensure a single owner-ship responsibility and is therefore broken.
Yes, I only wrote the SmartPtr class to test inheritance based type
conversion, I am not actually using the class. That's why its also missing
operator->() and operator*(). This test was inspired by an article by Scott
Meyers, which uses a very similar implementation of a smart pointer class:
http://www.aristeia.com/Papers/C++ReportColumns/sep96.pdf
The article is only a draft, so it may not be entirely accurate, but what
confuses me is that he writes that in order for the member function template
that performs the type conversion to compile, it must be legal to convert
the pointer to the derived class to a pointer to a "public or protected"
base class. And as I did not understand how this is supposed to work with a
protected base class, I wrote my testing code and, much to my surprise,
found out that on my compiler, it even works with a private base class, but
now I know that this is a defect. Would it also be a defect if in my
example, Base was a protected base class of Derived, and the compiler would
not reject the code?
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]