struct S {
S() {}
S (const S &) = delete;
};
void f1 (S s) {}
void f2 (S &s) {}
int main() {
S s;
f2(s);
}
Since S(S &s) is deleted, why does using f2 not throw an error since when it was declared it passes in the arguments S &s? When I use f1(s) it throws an error. I’ve looked at the definition of deleted functions and I thought this would throw an error, but it doesn’t. Why?
Lets have a look at
S:What you have here is a type that can be constructed, but cannot be copied.
Now lets take a look at your functions:
When you call
f1(s), the function tries to create a copy ofs– but your typeSforbids copying – that’s why this does not work.When you call
f2(s), the function creates a reference to its parameter – so whatever you do insidef2withs2is done directly to the original objects. There is no way a class can prevent anybody to take a reference of the object.