By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

about Vector class

P: n/a
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[i].getNum()<<" : ";
}
cout << endl;
}

void createCards(vector<Card> & p)
{
for(int i=0; i < 52 ; i++)
{
p[i] = 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

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.