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