0

Code:

int x = 2;
int* pointerone = &x;
int* pointertwo = pointerone;

So the address of pointerone is assigned to pointertwo, but is the copy constructor being called and the object that holds the address is copied into the address object of the other pointer, like all the default copy constructors on other types performing a shallow copy. If it is as I expect, how does the copy constructor of a pointer look like?

mrflash818
  • 930
  • 13
  • 24
  • 3
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jul 07 '17 at 13:19
  • `int` is a primitive type - it doesn't have a constructor. If you were instead using an object such as `string`, then by declaring a second pointer to it you wouldn't be calling its constructor. Only one copy of the object would exist. – jsheeran Jul 07 '17 at 13:23

3 Answers3

4

There are no constructors involved here. In fact this code is pure C code.

int x = 2;

// pointerone points to the memory address of x
int* pointerone = &x;

// pointertwo points to the same address than pointerone,
// therefore it points also to the memory address of x
int* pointertwo = pointerone;

That's all.

And even in C++, pointers don't have constructors, exactly as the int type doesn't have a constructor.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • If they do not have any constructors, how are they being assigned a value? Like what is holding the value they represent? –  Jul 07 '17 at 13:41
  • 2
    @DownRe Pointers and objects are two different concepts. Pointers are used to refer to objects. Objects have constructors, pointers do not. – François Andrieux Jul 07 '17 at 13:43
  • @FrançoisAndrieux but since objects are entities occupying memory, and pointers do occupy memory, aren't pointers considered as objects? :-/ –  Jul 07 '17 at 13:46
  • @DownRe Technically a pointer can be considered an object, so my comment may not have been totally accurate as it was written (not all objects have constructors, though they often do). But it's important not to confuse the object the pointer *is* with the object the pointer *points to*. Perhaps the confusion stems from a belief that the constructor is responsible for allocating memory, which is not the case. A constructor is responsible for initializing an object after it's memory has been allocated. – François Andrieux Jul 07 '17 at 13:50
  • @DownRe a pointer is just a memory address (typically a 32 bit number on a 32 bit system or a 64 bit number on 64 bit system), nothing more and nothing less. You can read about pointers in any decent C or C++ book. Google may help to, google "C pointers", there are tons of tutorials out there. – Jabberwocky Jul 07 '17 at 13:57
  • @DownRe: Yes, a pointer is an object. But it is not a _class_ object, so it does not have "constructors". It doesn't really matter, though; the end result is the same. – Lightness Races in Orbit Jul 07 '17 at 14:16
0

You could pretend, for symmetry, that the pointer's copy constructor is being invoked (and this would involve the copying of a single numerical "memory address").

In reality, pointers are of a built-in type and don't have constructors per se. The compiler has built-in logic that tells it what to make the program do.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

When you initialize a pointer with the value of another pointer you are just making another variable for the same memory address, as @Michael just said.

But be careful doing this. If you delete one of the pointer and you dereference the other one in another point of the program, it will raise an exception, and if not handled properly will crash your program.

Take this as an example:

int *ptr1 { new int(15) };
int *ptr2 { ptr1 }; // ptr2 points to same memory address as ptr1

// ... Some cool code goes here

delete ptr1; // Free the memory of ptr1

cout << *ptr2;
// Raises an exception because the address
// has been removed from the effective
// memory of the program.
Garmekain
  • 664
  • 5
  • 18