Why can't we use pass by pointer in copy constructor in c++?
Asked Answered
F

5

5

I know the general syntax of copy constructor in c++ would take reference. But, my doubt is what happens if we use pointer type instead of reference? Why don't we use pass by pointer mechanism in Copy constructor? What are the major disadvantages in it?

Flowerpot answered 12/1, 2015 at 8:59 Comment(3)
There is no problem with writing a constructor which takes a pointer instead of a reference. It's just not a copy constructor. It's a constructor which takes a pointer. You can just as well ask "Why can't we pass 7 strings, 12 ints and 3 vectors for the copy constructor?"Platas
A popular interview question in India :-)Endodontist
Duplicate of #18611975Woodwind
D
9

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.

Devito answered 12/1, 2015 at 9:8 Comment(0)
N
8

According to the standard, copy constructors taking pointers don't exist:

[C++11: 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 (8.3.6). [..]

Pointers are not included because the whole point of references is to "be" aliases for objects, whereas pointers represent indirection. Without delving too far into esoteric details about language design, it's a semantic distinction that makes a lot of sense when you consider the syntactic differences between them.

You can write any constructor you want, and it can take a pointer if you want it to, but it won't be a "copy constructor". It will be probably be really unclear and vague as to ownership semantics, and be unconventional, and be kind of weird, and that's why we don't do it.

It's not unheard of, though; consider this constructor that was the only way to instantiate a file stream in C++ until just three and a bit years ago:

 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
Nimbus answered 12/1, 2015 at 9:11 Comment(1)
That example is kind of misleading since it includes both copy and move constructors.Shippee
O
5

Technically, you could write a constructor that takes a pointer (although it's technically not a copy constructor in that case, based on the wording of the spec). However, this prevents you from using non-addressable results. Consider we have a big math class (multiprecision):

bigmath a = 14;

bigmath answer(a * 3);

You can't take the address of the expression a * 3, so you still need a const bigmath & version of the object.

(As Bart says, it also ensures that the object is a "proper object", which isn't going to hurt either - but to me, the above is a stronger argument).

I should perhaps add that the type(s) of constructors you choose to have for your object really depends on what the class does and represents. There are objects where a copy is definitely not "good", and there are other cases, like a bigmath class, where copy constructors are definitely a good thing. For a bigmath class, you'd probably also want a constructor that takes a long integer, a double and a string. In some cases, having a reference doesn't make sense. In some cases, having a const reference doesn't make sense. And in some cases, like I said, having a copy constructor doesn't make sense.

Typically, you can convert a pointer ptr to a reference by adding a dereference *ptr operation. Since the compiler will take the address of that (so that it can pass the reference), it won't add any extra code to use that. But for convenience, if you have a class that you often have pointers to and that you want to make copies of, then it may indeed make sense to have a constructor that takes a pointer. I'm not entirely sure where that would be, of the top of my head.

Obfuscate answered 12/1, 2015 at 9:9 Comment(4)
No, you couldn't. That would not be a copy constructor. The standard is clear about that.Nimbus
What, that doesn't compile, or it simply isn't a proper copy constructor if it takes a const bigmath & object?Obfuscate
@MatsPetersson: Refer to the standardese quoted in my answer. It doesn't need to be const bigmath& but a pointer is certainly not welcome.Nimbus
Sorry, got what you meant, and I have edited to correct the wording (twice, to make it even clearer)Obfuscate
T
0

A copy constructor is where you need the functionality to perform a deep copy or a shallow copy of the object thus we need to pass the reference of the values which are to be copied which acts as a fail safe to avoid junk values or nullptr to create a replica of the requested object. When you use pointers in the parameter, it just becomes a normal constructor.

More detail on this can be found in this article.

Triptolemus answered 6/1, 2023 at 0:28 Comment(0)
S
-3

According to me the standard of copy constructors use reference but reference and pointer both will give same output but whenever you will assign nullptr to object using reference(TestConstrutor obj2 = nullptr) it will not allow but if you are passing as a pointer, it will work but it will crash because you can not assign null pointer to object.

It will work fine :- TestConstrutor(const TestConstrutor &obj) x = obj.x; y = obj.y; //it will call TestConstrutor obj1(100,200); TestConstrutor obj2 = obj1; TestConstrutor(const TestConstrutor *obj)x = obj->x; y = obj->y;// it will call TestConstrutor obj3 = &obj2;

It will not allow :- TestConstrutor(const TestConstrutor &obj)x = obj.x; y = obj.y;//TestConstrutor obj2 = nullptr;

It will alow butit will crash :- TestConstrutor(const TestConstrutor &obj)x = obj.x; y = obj.y; TestConstrutor(const TestConstrutor *obj)x = obj->x; y = obj->y; // TestConstrutor obj2 = nullptr;

Sternutation answered 13/2, 2020 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.