Re: Some misc C++ questions (multimap, derived class function argument,
virtual static)
Digital Puer wrote:
On Sep 13, 12:37 pm, Pete Becker <p...@versatilecoding.com> wrote:
Digital Puer wrote:
However, apparently I cannot have a virtual static method
in C++ (or in Java). What is the best way to force that all
derived classes have such a factory method?
Specification and testing. Beyond that, why do you care? If someone
wants to derived from your class but not provide a factory, what harm
does it do?
Because I want to enforce the existence of a factory method
in all derived classes.
You already said that. The question I asked, rephrased a bit, is, what
do you gain by forcing all derived classes to implement this?
Every derived class will have its own
data, and I want the class to be able to produce a Base *
though the factory. I would like this factory method to be
usable by a consumer of the Base class:
Yes, that's clear. What if someone else (including you, in a different
application) uses your class and doesn't care about this? Why should
they have to write something they don't want?
void consumer()
{
Base *randomItem = Derived::createInstance(); // factory
doSomething(randomItem);
}
You are right that I could write an English specification that
says that all derived classes must have a factory method,
You still haven't said why it's a requirement that every class derived
from yours must provide a factory method. Yes, your application design
calls for factory methods, but that's part of the application's design,
not part of the class's design. Putting otherwise unnecessary
constraints on types in order to satisfy an application's design
sacrifices reusability.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)