473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

about Vector class

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
1 5578
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
3
by: not telling | last post by:
I have a vector class and a matrix class. The matrix class uses the vector class to implement a matrix. Within the vector class there is a polynomial curve fit method. The curve fit method uses...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
8
by: Sowen | last post by:
Hi, I have an object "elem", there are only simple functions inside, like setName, getName, and three constructors Now I have another class "Base", need an array of elem to initialize class...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
8
by: Tom | last post by:
I was reading Bjourne's book and wonder about constructors? If I have a class template<class T> class Vector { public: explicit Vector(size_t n); } Does a default constructor get...
3
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.