In C++ I have a constructor that accepts an object of class descriptor. This class has recently grown in size and I need to pass it by reference more. If I pass it by reference into the following ctor will the member still make its own copy given that it is not declared as a reference type?
descriptor taskedStats;
public:
CWorkerThread(int ticketNumber, int threadNumber, descriptor taskedStats) :
_pPaginatableForm(pPaginatableForm), ticketNumber(ticketNumber)
, threadNumber(threadNumber), taskedStats(taskedStats) {}
or
descriptor taskedStats;
public:
CWorkerThread(int ticketNumber, int threadNumber, descriptor &taskedStats) :
_pPaginatableForm(pPaginatableForm), ticketNumber(ticketNumber)
, threadNumber(threadNumber), taskedStats(taskedStats) {}
The default, or implicit, compiler-supplied copy constructor makes shallow copies of all members and would have the following signature:
so passing a reference to descriptor would be no different to attempting (in C parlance) to pass a copy- they would both in fact just be passed by reference.
If the member was declared as a reference type then, just as for pointers, its type wouldn’t match in the above, implicit, copy constructor and a reference assignment would be performed, thuse invalidating any attempt to create a local copy in each object.