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<iostre am>
#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(vect or<Card> & p)
{
for(int i=0; i < 52 ; i++)
{
cout << p[i].getNum()<<" : ";
}
cout << endl;
}
void createCards(vec tor<Card> & p)
{
for(int i=0; i < 52 ; i++)
{
p[i] = Card(i+1);
}
}
void editPost1Card(v ector<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 1 5611
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 go to great
lengths to restructure my code just to look concise and make it more
manageable.
When I say this, I'm also referring to the way I write my functions.
It seems to me sometimes that I shouldn't have many void functions
accepting...
|
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 matrices to implement perform the curve fit.
Does the fact that the vector class uses the matrix class violate a design
rule?
|
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)
|
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 Base
{
public:
|
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 CMyClass());
//???? Required ??????????????//
| |
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, typename V>
Vector<U_or_V> operator+ (
const Vector<U>&,
const Vector<V>&);
|
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 generated by the compiler?
|
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 default arguments.
In my class, the operator = works for another vector (just copying the
elements), and for a double: in this cases each element of the vector
will be equal to the double. Example:
|
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
vector_static_function.c:21: error: expected constructor, destructor,
or type conversion before '.' token
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |