User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,053 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,448 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 276 | Replies: 9
Reply
Join Date: Jun 2008
Posts: 74
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

vectors question

  #1  
Jul 7th, 2008
can I used an element like characters[2] to create an object out of it like an object from the class other character would be

OtherCharacter : characters[2]

I need to dynamically be able to create objects for the amount of characters in the vector or is their a better way of doing this.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,694
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: vectors question

  #2  
Jul 7th, 2008
you can do this:
vector<Character> characters;

The above will let you add as many Character class objects as you want to the vector.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: vectors question

  #3  
Jul 7th, 2008
so if I have a class telling the program how to build a character such as attributes and everything, I can just create objects of that class as many times as I need to using that code you posted?
Reply With Quote  
Join Date: Sep 2004
Posts: 6,065
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: vectors question

  #4  
Jul 7th, 2008
Well, you also need to add the objects to the vector using something like characters.push_back ( my_character ); .
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: vectors question

  #5  
Jul 7th, 2008
ok but when creating objects I do not no how to append something to the name of the object to differentiate it from the others because at one point 3 people could be logged into my game and at another 7 people could be so I have to be ready to know how to create objects dynamically if you no what I mean
Reply With Quote  
Join Date: Sep 2004
Posts: 6,065
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: vectors question

  #6  
Jul 7th, 2008
>if you no what I mean
No, I don't. It seems like you don't feel your question has been answered adequately. If that's the case, post your code so we can see what you're doing now and can get a better idea of what you might be asking for.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: vectors question

  #7  
Jul 8th, 2008
I have no idea where to start with this concept, I will explain it deeper so maybe you can get a feel for what I am trying to ask.

Ok so I have a class called OtherCharacter and its filled with all the attributes that have to be loaded by the client when there is other characters in the game. But sometimes it is five people and sometimes its 20, so I need a way even if It is not vectors to create an object of the othercharacter class for every person on the server. I don't need the networking part of it just how to create an object dynamically almost.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,065
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: vectors question

  #8  
Jul 8th, 2008
I think you're confused. Just use vectors where you need more than one instance of any kind of data. When you create a character, also add that character to the list of "other characters". It doesn't seem like a terribly difficult thing to me, but then again, you're still not being very clear.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,694
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: vectors question

  #9  
Jul 8th, 2008
as previously mentioned, vector class doesn't care how many items you put into it, nor does it care when you do it. You can put an item into the vector at any random times. So when someone logs onto your game, fills out all the information needed for that character class, just add an instance of that class into the vector. The vector class will grow by itself as you add more objects to it.

Mabe what you want is a quick tutorial (click here)
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2008
Posts: 74
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: vectors question

  #10  
Jul 8th, 2008
thanks ill try your suggestions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC