about Vector class

S

Steven Lien

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
 
K

Kevin Goodsell

Steven said:
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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top