0

I just want to make sure if I understand the properties of pointers. So if I have something like this:

#include <iostream>
using namespace std;

class Person
{
public:
  Person(){myBook = new Book(4);}
  void printPerson()
  {
    int i =0;
    while(i<n)
    {
        cout<<myBook[i].n<<endl;
        i++;
    }
  }

private:
   Book *myBook;
   int n;
};

class Book
{
public:
  Book(int num)
  {
      int n =0;
  }  
  int n;
  };

Since the instance of Person class is a pointer, when I try to make a copy constructor and assignment operator=, I have to allocate a new Book for the new Person object. am I right? thx

  • Where is your instance of Person? What's in your main? – alestanis Feb 14 '13 at 22:12
  • 1
    What are you expecting that `while` loop to do? What are you expecting `n` to be? – Joseph Mansfield Feb 14 '13 at 22:13
  • 1
    In most cases, yes. I wouldn't make the blanket statement that it must _always_ be done so. Depending on the implementation, you might want multiple instances of Person having a pointer to the same instance of Book. – Mustafa Ozturk Feb 14 '13 at 22:13
  • @mustafa ozturk if multiple instances of person have a pointer to the same instance of Book, what will happen to the when the person obj is destructed? eg, when person a,b,c is pointing to book x. when person a is destructed, the book x is destructed. So, don't you think person b and c will pointing to an object that didn't exist anymore? – Rudi Aksam Feb 14 '13 at 22:17
  • I would be more concerned about the strange `while` loop you are doing the pointer. – juanchopanza Feb 14 '13 at 22:17
  • @RudiAksam If multiple instances of the class are sharing a resource then the destructor of the class has to be intelligent in managing that resource. The class could count, using a static variable, the number of instances currently created and delete the shared resource only when the count goes to zero. Indeed, in C++11 there is a smart pointer, shared_ptr, which does this all for you automatically. I wrote up a quick example [here](http://ideone.com/v1Cju8) – Mustafa Ozturk Feb 19 '13 at 14:29

2 Answers2

4

There is a bug here:

Person(){myBook = new Book(4);}

allocates one Book object with num=4, if you mean to allocate 4 Book elements, do:

Person(){myBook = new Book[4];}

Otherwise when you access myBook[i] in while loop is undefined behavior:

while(i<n)
{
   cout<<myBook[i].n<<endl;
               ^^^ ouch
   i++;
}

Since the instance of Person class is a pointer, when I try to make a copy constructor and assignment operator=, I have to allocate a new Book for the new Person object. am I right?

Yes, make sure you follow rule of three

Other suggestions are, use std::vector instead of dynamic array, use smart pointers instead of raw pointers if you could.

Community
  • 1
  • 1
billz
  • 44,644
  • 9
  • 83
  • 100
1

(under the assumption, you meant instance of book, not of person)

In order to make a deep copy of a person, you need to create a new book instance as well. Using a deep copy allows you to destroy the original person and its book while preserving the copy and the copied book.

There some use cases, where you actually want both persons to point to the same book object.

In such cases you need to keep track how many pointers are pointing at the book using somthing like a shared pointer.

Edit to clear it up: If you have more than one Person with the same Book, and you have a valid destructor on the person:

class Person {
public:
  ~Person() {
    delete m_pbook;
  }
protected:
  Book *m_pbook;
}

class Book {
  int foo;
}

Person a = new Person();  // book pointer is something like 0x12345678
Person b = new Person(a); // book pointer is identical 0x12345678

// delete a yields
delete a; // memory in 0x12345678 is freed up
b->book;  // AccessViolation / Segmentation Fault, the memory in 0x12345678 does not belong to your program any more.
scones
  • 3,317
  • 23
  • 34
  • if multiple instances of person have a pointer to the same instance of Book, what will happen to the when the person obj is destructed? eg, when person a,b,c is pointing to book x. when person a is destructed, the book x is destructed. So, don't you think person b and c will pointing to an object that didn't exist anymore? – Rudi Aksam Feb 14 '13 at 22:20
  • 1
    @RudiAksam no, your assumption is incorrect. When a person is destructured, the book is not automatically destructed. You have to tell the compiler what to do in the Person destructor. You could delete the book, decrement a reference count, or hold the book by a shared_ptr. – Brian Neal Feb 14 '13 at 22:22
  • @David I understood his question to be about the behaviour of the default copy constructor when dealing with pointer member variables. The outer object does not need to be a pointer for the bare minimum example. In fact it would just make it more complicated. – scones Feb 14 '13 at 23:01