Q
Q
Qubc2021-08-28 22:41:36
C++ / C#
Qubc, 2021-08-28 22:41:36

Why can't the constructor Class ( Class other) { } take an object of its type by value?

Assumptions: A constructor is essentially a function that returns nothing, gets the address of its object implicitly, and gets something else in the parameter list explicitly. Parameters are allocated on the calling function's stack and used by the called function.
Why doesn't this logic work with constructor? Just because or there are some complicated reasons? It is clear that this is inefficient and pointless.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2021-08-28
@Qubc

Because in order to pass any argument by value, it must be copied. To copy an argument that is an object of a class, you need to call the copy constructor (whether automatically generated or manually written).
Thus, to "go inside" the copy constructor, to which the argument is passed by value, you need to call ... the copy constructor. of the same class. Those. the same constructor.
Like it should be clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question