about Vector class

Discussion in 'C++' started by Steven Lien, Aug 14, 2003.

  1. Steven Lien

    Steven Lien Guest

    I wrote a simple vector class that can store Cards, and my question is
    if i had Card class, should i "new" it to store into vector , or simply use
    copy structure?.
    if i need the vector class to be dynamic?...

    IE:

    vector<Card>* p = new vector<Card>(52);

    or

    vector<Card* >* p = new vector<Card* >(52);



    i've tried to play around vector class,
    vector<Card>* p = new vector<Card>(52);
    seems the Card objects i've created are dynamically....
    in other word, pass dynamic vector "p" to a function and change the card
    data,
    p will have keep newest data....

    Why that is the case?


    see the following source code
    ----------------------------------------------------------------------------
    --------------------

    #include<iostream>
    #include<vector>
    using namespace std;

    class Card
    {
    private:
    int num;
    public:
    int getNum()
    {
    return num;
    }
    void setNum(int m)
    {
    num = m;
    }
    Card()
    {
    num = -1;
    }
    Card(int m):num(m)
    {

    }
    virtual ~Card()
    {

    }
    };

    void printCards(vector<Card> & p)
    {
    for(int i=0; i < 52 ; i++)
    {
    cout << p.getNum()<<" : ";
    }
    cout << endl;
    }

    void createCards(vector<Card> & p)
    {
    for(int i=0; i < 52 ; i++)
    {
    p = Card(i+1);
    }
    }


    void editPost1Card(vector<Card> & p)
    {
    p[0].setNum(150);
    }

    int main()
    {


    vector<Card>* p = new vector<Card>(52);
    createCards(*p);
    printCards(*p);
    editPost1Card(*p);
    printCards(*p);

    system("pause");
    }
    -------------------------------------------------------------
    output

    1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : 13 : 14 : 15 : 16 : 17 :
    18 :
    19 : 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30 : 31 : 32 : 33 :
    34 :
    35 : 36 : 37 : 38 : 39 : 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 : 48 : 49 :
    50 :
    51 : 52 :

    150 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : 13 : 14 : 15 : 16 : 17
    : 18
    : 19 : 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30 : 31 : 32 : 33
    : 34
    : 35 : 36 : 37 : 38 : 39 : 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 : 48 : 49
    : 50
    : 51 : 52 :
    ----------------------------------------------------------------
    i've change postion 0 in the vector to 150
    even i don't "new" Card

    Any help will be appreciated...
    Thank you
     
    Steven Lien, Aug 14, 2003
    #1
    1. Advertising

  2. Steven Lien wrote:

    > I wrote a simple vector class that can store Cards, and my question is
    > if i had Card class, should i "new" it to store into vector , or simply use
    > copy structure?.
    > if i need the vector class to be dynamic?...
    >
    > IE:
    >
    > vector<Card>* p = new vector<Card>(52);
    >
    > or
    >
    > vector<Card* >* p = new vector<Card* >(52);


    There doesn't seem to be a good reason for either of these. What's wrong
    with:

    vector<Card> vec(52);

    ?

    >
    > i've tried to play around vector class,
    > vector<Card>* p = new vector<Card>(52);
    > seems the Card objects i've created are dynamically....
    > in other word, pass dynamic vector "p" to a function and change the card
    > data,
    > p will have keep newest data....
    >
    > Why that is the case?


    I'm not sure I understand, and I'm pretty sure you are using the word
    "Dynamic" incorrectly.

    If you pass a pointer to a function, and in that function modify the
    thing pointed to by that pointer, of course the changes will be visible
    in the calling function. That's basic C++. If you don't understand that
    much, then you are getting way ahead of yourself with this program. You
    need to learn the basics before you try to write something non-trivial.

    <snip code>

    > i've change postion 0 in the vector to 150
    > even i don't "new" Card


    See above. This has absolutely nothing to do with 'new'. You don't need
    'new' and there's at least 2 reasons you should not be using it: 1) It
    is unnecessary in this program and can only cause problems. 2) You don't
    understand pointers yet. Basics first.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 14, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    8
    Views:
    856
    Dhruv
    Jul 2, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,855
  3. Replies:
    8
    Views:
    1,940
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    369
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page