Why is the argument of the copy constructor a reference rather than a pointer?
Why can't we use the pointer instead?
Why is the argument of the copy constructor a reference rather than a pointer?
Why can't we use the pointer instead?
There are many reasons:
References cannot be NULL. OK, it's possible to create a NULL reference, but it's also possible to cast a std::vector<int>*
into a std::vector<SomeType>*
. That doesn't mean such a cast has defined behavior. And neither does creating a NULL reference. Pointers have defined behavior when set to NULL; references do not. References are therefore always expected to refer to actual objects.
Variables and temporaries cannot be implicitly converted into pointers to their types. For obvious reasons. We don't want pointers to temporaries running around, which is why the standard expressly forbids doing it (at least when the compiler can tell you are doing it). But we are allowed to have references to them; these are implicitly created.
Because of point number 2, using pointers rather than references would require every copy operation to use the address-of operator (&). Oh wait, the C++ committee foolishly allowed that to be overloaded. So any copy operation would need to actually use std::addressof
, a C++11 feature, to get the address. So every copy would need to look like Type t{std::addressof(v)};
Or you could just use references.
It's just nomenclature. You can use a pointer as well, but that'd be called a conversion constructor.
If you think about it, it makes sense, because you copy one object to another (ergo the "copy"). Not from a pointer to an object. If it was a pointer, it wouldn't do a copy, because you're not copying the pointer to the object, but rather the object the pointer points to to your object.
It's just nomenclature
- I'm disagree with it. If you have conversion constructor - copy constructor will be implicitly defined, that can be not what you want. –
Bloodyminded copy constructor
is one of the big three. If you don't define a conversion constructor
as you call it someone who attempts to use it will likely get a compiler error (might depend on if you use explicit
or not) , where as a copy constructor will get created for them that could be different from your conversion constructor
.. Not to mention most any STL or templated function or class interacting is going to expect the copy constructor
to work properly. –
Bimolecular Why should it be a pointer? A null pointer wouldn't make sense. And using a pointer wouldn't allow copying temporaries, so you couldn't do things like:
MyClass
func()
{
// ...
return MyClass(...);
}
You can define a constructor taking a pointer. But it won't be a copy constructor, because it couldn't be used in cases like the above. And it won't inhibit the compiler from generating a copy constructor.
Because this is denied by the standard.
Quoting from C++ draft standard n3376 - section 12.8.2:
A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments
Because a pointer could be a nullptr
which would have to be checked and temporaries can't have pointers to them.
Passing by references ensures an actual object is passed to the copy constructor, whilst a pointer can have NULL value, and make the constructor fail
© 2022 - 2024 — McMap. All rights reserved.
Foo::Foo(Foo const *)
, but it's not a copy constructor. GivenFoo a;
a copy construction looks likeFoo b(a);
orFoo c = a;
(and in C++11 maybeFoo d{a};
). Now considerFoo x(&a);
orFoo y = &a;
, does it look like a copy to you?.. – Elliott