I am reading some clone c++ implementation, it seems always define as
Clonable* clone();
I am wondering why always return a pointer; can I define a clone to return a Clonable object?
I am reading some clone c++ implementation, it seems always define as
Clonable* clone();
I am wondering why always return a pointer; can I define a clone to return a Clonable object?
 
    
     
    
    can I define a clone to return a
Clonableobject?
Sure. Heck, you could make a clone() method that returns anything you want. My guess is that the Clonable class is meant to be used polymorphically, and that the clone() method is virtual. Pointers (or references) are your only option for runtime polymorphism in C++. If it didn't return a pointer, you'd get object slicing.
 
    
    