mangenouilles mangenouilles - 1 year ago 90
C++ Question

class constructor with const reference

in this class

class Foo {
Foo(Foo const &) {}

when would
Foo(Foo const &)
ever be called? I do not understand this kind of constructor

Answer Source

This is called copy constructor. When you want to initialize a class instance by copying an existing instance, you'd probably want to use it.

const - you don't want to change the object you are copying.

reference - you don't want to copy the existing instance into the constructor, just to copy it again in the initialization.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download