Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Constructor with Heap Pointers

Tags:

c++

pointers

I'm trying to make a pointer call the copy constructor on creation but keep referencing the object instead it seems. Is what I'm doing completely wrong then.

Queue<int> * a = new Queue<int>();
Queue<int> * b = a;

This keeps referring to a instead of using the copy constructor which works fine on stack allocated objects.

like image 288
Crossman Avatar asked Apr 13 '26 14:04

Crossman


1 Answers

You can't call copy constructors through pointers like that.

To call the copy constructor you need to be more explicit:

Queue< int >* b = new Queue< int >( *a );