Copy constructor: deep copying an abstract class

元气小坏坏 提交于 2019-12-03 23:48:54

You could add a clone() function to your interface.

You'll have to add that code yourself to the Material copy constructor. Then code to free the allocated IColor in your destructor.

You'll also want to add a virtual destructor to IColor.

The only way to do a deep copy automatically would be to store a color directly instead of a pointer to an IColor.

Adding a clone() method to color is probably best, but if you don't have that option, another solution would be to use dynamic_cast to cast IColor* to Color*. Then you can invoke the Color copy constructor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!