0

I have a confusion about the pointers. as the code shows below, CreditCard is a class defined in the head file, and we're defining a vector that contains 10 CreditCard pointers. After defining, we assign 3 new cards to the vector. It is clear that the vector's elements should be type pointer to CreditCard, however we actually assign CredicCard objects rather than pointers to it. Besides, what is "new" infront of each CreditCard? Could anyone explain it to me? Thank you!

( the code is from Data Structures and Algorithms in C++ 2nd Edition by Michael T. Goodrich (Author), Roberto Tamassia (Author), David M. Mount (Author) )

vector<CreditCard*> wallet(10); // vector of 10 CreditCard pointers
// allocate 3 new cards
wallet[0] = new CreditCard("5391 0375 9387 5309", "John Bowman", 2500);
wallet[1] = new CreditCard("3485 0399 3395 1954", "John Bowman", 3500);
wallet[2] = new CreditCard("6011 4902 3294 2994", "John Bowman", 5000);
kaiyu wei
  • 431
  • 2
  • 5
  • 14
  • 1
    `new` - _"...Creates and initializes objects with dynamic storage duration, that is, objects whose lifetime is not necessarily limited by the scope in which they were created...."_ , "_...The new-expression returns a (prvalue) pointer to the constructed object ..."_ https://en.cppreference.com/w/cpp/language/new – Richard Critten Nov 27 '21 at 13:47
  • New allocates memory for an object on the heap. However I wouldn't use vector this way, I would use std::vector and wallet.emplace(("5391 0375 9387 5309", "John Bowman", 2500) and let vector take care of all the memory managment for me. In modern C++ the use of new/delete shouldn't be used a lot. – Pepijn Kramer Nov 27 '21 at 13:52
  • @PepijnKramer A bit too short; not only allocating memory (this is what `operator new` does, but that should not be confused with a `new` expression), but additionally constructing the object (i.e. call the constructor). – Aconcagua Nov 27 '21 at 13:55
  • You might want to peek into a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), that should get explained there pretty early... – Aconcagua Nov 27 '21 at 14:02
  • @aconcagua yup you are correct. New does that too – Pepijn Kramer Nov 27 '21 at 15:26

1 Answers1

2

After defining, we assign 3 new cards to the vector. It is clear that the vector's elements should be type pointer to CreditCard, however we actually assign CredicCard objects rather than pointers to it.

No we are not assigning 3 new cards to the vector. Instead we are assigning the pointers to CreditCard objects created on heap. Take the below example into consideration:

Case I

new CreditCard("5391 0375 9387 5309", "John Bowman", 2500);

The above statement does two things:

  1. creates a CreditCard object on the heap
  2. returns a pointer to that created object

Case II

Now lets take a look at the statement in your code snippet:

wallet[0] = new CreditCard("5391 0375 9387 5309", "John Bowman", 2500);

This statement does three things:

  1. creates a CreditCard object on the heap
  2. returns a pointer to that created object
  3. assign the pointer that was returned in step 2 to wallet[0]

Case III

Similarly,

wallet[1] = new CreditCard("3485 0399 3395 1954", "John Bowman", 3500); 

involves 3 things:

  1. creates a CreditCard object on the heap
  2. returns a pointer to that created object
  3. assign the pointer that was returned in step 2 to wallet[1]

You can check out Objects on a stack vs Objects on a heap in C++ for more about heap and stack.

Also, note that when you're creating objects on the heap and not using smart pointers, then you must free the memory using delete.

More Examples

Creating Objects on Stack

//create a vector of CreditCard objects instead of creating a vector pointers to CreditCard objects
std::vector<CreditCard> wallet(10); //create a vector of size 10 of CreditCard objects

wallet[0] = CreditCard("5391 0375 9387 5309", "John Bowman", 2500);
wallet[1] = CreditCard("3485 0399 3395 1954", "John Bowman", 3500);
wallet[2] = CreditCard("6011 4902 3294 2994", "John Bowman", 5000);

Note in the above code snippet i have not used the keyword new. So we create 3 CreditCard objects and then assign those objects to wallet[0], wallet[1], wallet[2].

Creating objects on Heap

//create a vector of pointers to CreditCard objects instead of creating a vector of CreditCard objects
vector<CreditCard*> wallet(10); // vector of 10 CreditCard pointers

// Create 3 CreditCard objects on heap and then assign the pointer returned to them to the left hand side`
wallet[0] = new CreditCard("5391 0375 9387 5309", "John Bowman", 2500);
wallet[1] = new CreditCard("3485 0399 3395 1954", "John Bowman", 3500);
wallet[2] = new CreditCard("6011 4902 3294 2994", "John Bowman", 5000);

In the above code snippet, the we have used the keyword new(unlike last example). The effect of using the keyword new is that we create an object on the heap and then a pointer to that object is returned which is assigned to wallet[0], wallet[1], wallet[2].

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Thank you for your explaination. But I still don't understand why this code returns a pointer instead of an object? or, what if I want to return an object here, how would the code be like? – kaiyu wei Nov 27 '21 at 14:16
  • @kaiyuwei I have added some more examples at the end of my answer. Check it out. I have also added an example where we don't return a pointer but an object itself as you want. – Jason Nov 27 '21 at 15:00
  • That's much clearer! I think my confusion is mainly about the use of "new" expression. Since I'm a C++ beginner I really need to learn more about it. – kaiyu wei Nov 28 '21 at 14:10