I just don't work with pointers very much because when I do they
tend to lead to problems. I always thought the refrence thing was a
good a a pointer and much safer.
I'm going to put this bluntly: if you want to program C/C++
professionally, then you better be proficient in using pointers. Even
if you've botched things in the past or swallowed the line that
references are "as good," you're going to have to learn how to use
pointers. Memory management is a required skill for C/C++ programmers,
and the only way to effectively do that in the core language is with
pointers.
A reference is *not* equal to a pointer. There's a whole LOT of
ways they're different. The most obvious one is that references must
be initialized when defined, so that they point to valid data. A
pointer has no such restriction. As has already been pointed out, you
can't make a STL vector of references. Further, to allocate any
memory, you MUST account for the possibility that a memory allocation
failed. And that violates the #1 rule of references, that they point
to something valid.
Getting back to your code, the above raises a serious issue you're
going to need to sit down and think about for a while. Specifically,
memory management. Your map can have a fixed maximum size, that's not
serious and can be designed around. However, what goes *on* your map
isn't fixed. Assuming you have 'class Unit { ... };' for the things
placed on the map, the Units don't magically just appear in
memory. You *MUST* think through these questions:
1) When is a Unit created? By what function?
2) Who "owns" that memory?
3) When/how is that memory released?
So far, I guess you're doing something like this:
void DoSomething(void)
{
Unit unit1
DoSomethingElse(unit1);
}
In this function, 'unit1' is created, for free, on the stack. [If
you don't know what that is, please sit down with C++ reference
manuals and
tutorials.
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html is
one good free reference, available for download at
http://www.planetpdf.com/developer/article.asp?ContentID=6634 among
other places] However, at the end of DoSomething() [i.e., when it goes
out of scope], unit1 is gone. Destroyed.
In order to properly create arbitrary numbers of units on your map,
you need to create them with the new operator. That doesn't allocate
on the stack, and instead gives you a pointer you can use. That
pointer should be passed to the MapManager instead, as I've
recommended from the beginning.
I know that this is basic c++ stuff but if I do,
void mapmgt::atIn(unit& u){
int x = u.getXloc();
int y = u.getYloc();
h[y][x].at.push_back(u);
}
Complete style nitpick: if you want anyone else to read your code,
then do NOT abbreviate this much. It makes your code a LOT less
readable. Write your header as follows:
void mapmgt::atIn(unit* u)
or, better yet
void MapManager::AddAttacker(Unit* u)
for(int lp = 0; lp < rteam.size(); lp++){
m.atIn(rteam[lp]);}
How do I read that as a pointer?
You've omitted all the definitions of these variables, and due to
overabbreviation, it's nearly impossible to figure out what these
variables are, and what you're trying to achieve here. Your map
manager should probably be a singleton, and a pointer retrieved. For
example
class MapManager
{
// ...
public:
static MapManager* Get(void);
//...
private:
static MapManager* s_Instance;
}
MapManager* MapManager::Get(void)
{
if(s_Instance = NULL)
s_Instance = new MapManager;
_ASSERTE(s_Instance); // Or some other form of error management
return s_Instance;
}
Then, in other code:
void SomeFunc(...)
{
MapManager* pMapManager = MapManager::Get();
Unit pAttacker1 = new Unit;
pAttacker1.SetPosition(5,5);
pMapManager->AddAttacker(pAttacker1);
}
Nathan Mates