Copy constructors and defensive copying

前端 未结 5 1449
梦谈多话
梦谈多话 2020-12-03 10:44

What is a copy constructor?

Can someone share a small example that can be helpful to understand along with defensive copying p

5条回答
  •  借酒劲吻你
    2020-12-03 11:31

    This is where you create a new object, by passing an old object, copying its values.

    Color copiedColor = new Color(oldColor);
    

    instead of :

    Color copiedColor = new Color(oldColor.getRed(),
                                  oldColor.getGreen(), oldColor.getBlue());
    

提交回复
热议问题