简体   繁体   中英

Problem about c++ pointers assigning values

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);

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM