Polymorphic copy-constructor with type conversion

后端 未结 4 1617
遇见更好的自我
遇见更好的自我 2021-01-26 11:18

I need to copy-construct an object simultaneously changing it\'s type to another class being a member of the same class-hierarchy. I\'ve read about polymorphic copy-constructors

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-26 11:54

    Simplest solution would probably be to implement a Child2 constructor taking a Child1& as parameter. Then you could simply call:

    Child2* c2 = new Child2(*c1);
    

提交回复
热议问题