On Thursday, 30 December 2021 at 01:04:10 UTC, Ali Çehreli wrote:
The second item in the documentation mentions "any number of default parameters" when describing copy constructor syntax:

  https://dlang.org/spec/struct.html#struct-copy-constructor

1) I can't figure out how to use those extra parameters. For example, I can't find a special function name to call explicitly:

  S.__cctor(a, 42);  // No go

2) Unlike the examples there, I think the parameter should most usefully be defined as 'const' unless there is a special reason:

struct S {

  // const(S) instead of S:
  this(ref const(S) that) {
  }
}

Do you agree?

Thank you,
Ali






11

A copy constructor always takes one parameter, reference to the type for which it belongs, there maybe other parameters but they must have default values.

An copy constructor is called as an copying function and the purpose of the copy constructor is to create an object of a type by using an object of the same type as basis for creation of the new type.

The Standard specify's that the copy constructor be of the type:

T(const &T obj);
This basically allows creation of temporary objects during calling functions by value or returning objects of the type by value.
This syntax facilitates creation of an new object as:

T obj1(obj2);      <--------- Direct Initialization
T obj1 = obj2;     <--------- Copy Initialization
If the additional arguments being passed to the copy constructor would not be mandated to have default values then the construction of objects using the above syntax would not be possible.
Hence the strict condition,
there maybe other parameters to a copy constructor but they must have default values.



https://acatpg.mn.co/posts/19530612




Reply via email to