Add elememt(s) from object A to obect B

This is a discussion on Add elememt(s) from object A to obect B within the C++ Programming forums, part of the General Programming Boards category; Suppose I have two classes - Deck and Pile and I created 2 obects inside the Pile class Code: Deck ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    Add elememt(s) from object A to obect B

    Suppose I have two classes - Deck and Pile and I created 2 obects inside the Pile class

    Code:
    Deck MyDeck;          // Instantiate a Deck object. 
    	
    Pile MyPile;          // Instantiate a Pile object.
    If MyDeck contains 52 cards, What should I do if i want to add 13 cards from MyDeck to MyPile?


  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    MyDeck.copy13CardsTo(&MyPile);

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I see, this should be the format of copying the elements from object A to object B.

    Do I need to include some specific header?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just out of curiosity, what's the difference between a deck and a pile? Having separate Deck and Pile classes seems somewhat redundant to me.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Because i am writing a soliatire game
    The Deck class is those cards which the player click the upper-left corner.
    The Pile class is those card the user can place it from the Deck class.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Do you know any STL Lists method that i can copy the cards to object B?
    i am looking the MSDN guide, but still no idea..

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The Deck class is those cards which the player click the upper-left corner.
    >The Pile class is those card the user can place it from the Deck class.
    And the need for two classes is <speculation>because you failed to make the Deck class generic enough to handle "piles" of cards rather than "decks" of 52 poker cards</speculation>?
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by joenching
    Do you know any STL Lists method that i can copy the cards to object B?
    i am looking the MSDN guide, but still no idea..
    Assuming you have cards in a <vector>, you could use copy() like this:
    Code:
    int nums[]={1, 2, 3, 4, 5};
    
    vector<int> source(nums, nums+5);
    vector<int> destination(5);
    
    copy(source.begin(), source.begin() + 2, destination.begin() + 3);
    
    cout<<destination[4]<<endl; //2
    #include <vector>
    Last edited by 7stud; 05-01-2005 at 06:47 PM.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Would you pls explain (what source.begin() + 2, destination.begin() + 3) for?

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    source.begin() is what's called an iterator, which is a smart pointer or an object that acts like a pointer but hides the complexity of what it's actually doing, e.g. opening up three phone lines retrieving data over a network, organizing the data, and finally giving you a pointer to the data. So, source.begin() is like a pointer to the first thing in the vector. If you are familiar with pointer arithmetic, you can add integers to a pointer to move along the data when the pointer points to an array or vector. That works because the data for an array or a vector is contiguous in memory. So, source.begin() + 2 is a pointer to the 3rd element in the vector.

    destination.begin() + 3 is a pointer to the fourth element of the destination vector, and it is the spot where copying begins. Note that the range:

    source.begin(), source.begin() + 2

    includes the value at source.begin() but does not include the value at source.begin() + 2. source.begin() + 2 is a pointer to one element past the last element included in the range. So, the range there will include the thing at source[0] and source[1] but not source[2].
    Last edited by 7stud; 05-01-2005 at 10:02 PM.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Thanks for helping.
    it's quiet good.!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 05:26 AM
  2. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 02:08 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  4. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 06:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21