0

I need to do something that I think is called an Object Array. For example: A player accesses the server, this server will allocate this player as an object of such a group, so will have many objects of the same class.

Is there a way to make using one line for each player, but would have to be a source editing and compilation in real time and without stopping the server, which is impossible, but to demonstrate:

Player p0000001 ("Nickname", 150);
Player p0000002 ("OtherNickname", 17);
Player pNNNNNNN ("Nick", 00);

I wanted something like:

Player players[0].nickname = "Nickname";
Player players[0].level = 150;
Player players[1].nickname = "OtherNickname";
Player players[1].level = 17;

It's possible this? I just need a demo for me to adapt. Thanks, Bruno Alano.

Bruno Alano
  • 643
  • 1
  • 11
  • 21

3 Answers3

1

The best thing would be to use one of the STL containers such as std::vector:

#include <vector>

std::vector<Player> players;
players.push_back(Player("Nickname", 150));

A full reference to std::vector can be found on cplusplus.com.

You
  • 20,363
  • 3
  • 45
  • 63
  • Sorry, but I don't know use vectors **yet**. Are other method using class? – Bruno Alano Jul 07 '11 at 20:23
  • @Bruno. Time to learn, then. A vector is really just an array that's much more difficult to mess up with. And a vector *is* a class. – Roddy Jul 07 '11 at 20:24
  • 1
    @Bruno: Not really, assuming you're only going to *use* `std::vector`. The only contact with the template you'll be having is when creating a new vector, and when creating iterators. – You Jul 07 '11 at 20:28
  • 2
    @Bruno. "to know templates?". Only in the sense of using them, not writing them. Compare eating a meal, with cooking one from scratch. – Roddy Jul 07 '11 at 20:28
0

If I understood you right, you want to handle an unknown amount of players dynamically(meaning you cant hardcode it at compile time). That usually implies dynamic memory allocation with new/delete. As you don't know how many players there will be you should use a data structure of dynamic size, like std::vector or std::list.

If you use an std::vector/list try to handle pointers and not objects themselves because that triggers copy-constructors, destructors and therefore a whole lot of unnecessary operations for just holding these values.

  • Re pointers in `std::vector`, see [this question](http://stackoverflow.com/questions/141337/c-stl-should-i-store-entire-objects-or-pointers-to-objects). Depending on what you're doing, storing copies might be better. – You Jul 07 '11 at 20:29
  • From the efficient point of view I would say storing one pointer and creating one object per object is faster. Also I made some design mistake with this in my early days that later took me one week to track down, which was caused by copies being made in stl containers. Of course it was not the containers fault but it helped to hide my errors from me so I saw them too late. :) – Nobody moving away from SE Jul 07 '11 at 20:36
0

I'm not sure, but I think you need a C++ program monitoring an input file. Is that true? If it is, you just need the program to read each new line (i.e. while !eof). The client will open the file in append mode and will add the lines as new users join the server.

You can find some code here:

http://www.dreamincode.net/forums/topic/222664-how-to-read-continuously-refreshed-file-in-c/

Hope this helps.

Baltasarq
  • 11,266
  • 2
  • 34
  • 53