A
Asfand Yar Qazi
Hi,
I have the following header file in my 'everything useful I think of
in one place' library:
============= BEGIN CODE SNIPPET ===========
/** All classes that derive from this obtain a 'virtual constructor' -
ie if the 'clone()' method is called on a polymorphic type, an
object of the same (unknown) type is returned. */
class CloneService
{
public:
virtual ~CloneService()
{
}
/// This MUST be overridden in children classes. Abstract
/// classes might want to override this too (and keep it
/// abstract), as it will make it more convenient for users to
/// use pointers of those classes.
virtual CloneService*
clone() const = 0;
};
/// Makes it easy to check at compile time if a given template
/// parameter class has the 'clone' method.
template<typename T>
bool
check_clone_service_requirements()
{
T* (T::*test)() const = &T::clone;
test = test;
return true;
}
============= END CODE SNIPPET ===========
Could someone tell me the feasibility of having something like this?
I've used it in a few places and found it useful, but I always do
something like:
class Base : public CloneService
{
// ...
Base* clone() const = 0;
// ...
};
class Child : public CloneService
{
// ...
Child* clone() const {return new Child(*this);}
// ...
};
i.e. I have to redeclare the clone virtual method in the abstract
virtual base class definition anyway. Can anyone thing of any
situations where having a 'CloneService' base class for all cloneable
classes everywhere would be useful?
And I realise now that the name 'Cloneable' is a better one than
'CloneService' - but that's what using too much Java does to you.
Thanks,
Asfand Yar
--
Entry in RollerCoaster Tycoon 2 readme.txt file:
RollerCoaster Tycoon2 must be played on a video card capable of
640x480 screen resolution at a bit depth setting of 256 bits.
And the proof that playing too many strategy games causes loss of
humour: http://tinyurl.com/dyrtt
I have the following header file in my 'everything useful I think of
in one place' library:
============= BEGIN CODE SNIPPET ===========
/** All classes that derive from this obtain a 'virtual constructor' -
ie if the 'clone()' method is called on a polymorphic type, an
object of the same (unknown) type is returned. */
class CloneService
{
public:
virtual ~CloneService()
{
}
/// This MUST be overridden in children classes. Abstract
/// classes might want to override this too (and keep it
/// abstract), as it will make it more convenient for users to
/// use pointers of those classes.
virtual CloneService*
clone() const = 0;
};
/// Makes it easy to check at compile time if a given template
/// parameter class has the 'clone' method.
template<typename T>
bool
check_clone_service_requirements()
{
T* (T::*test)() const = &T::clone;
test = test;
return true;
}
============= END CODE SNIPPET ===========
Could someone tell me the feasibility of having something like this?
I've used it in a few places and found it useful, but I always do
something like:
class Base : public CloneService
{
// ...
Base* clone() const = 0;
// ...
};
class Child : public CloneService
{
// ...
Child* clone() const {return new Child(*this);}
// ...
};
i.e. I have to redeclare the clone virtual method in the abstract
virtual base class definition anyway. Can anyone thing of any
situations where having a 'CloneService' base class for all cloneable
classes everywhere would be useful?
And I realise now that the name 'Cloneable' is a better one than
'CloneService' - but that's what using too much Java does to you.
Thanks,
Asfand Yar
--
Entry in RollerCoaster Tycoon 2 readme.txt file:
RollerCoaster Tycoon2 must be played on a video card capable of
640x480 screen resolution at a bit depth setting of 256 bits.
And the proof that playing too many strategy games causes loss of
humour: http://tinyurl.com/dyrtt