Structured code incorrectly? References are bad choice in provided case? Help find solution. Fixed

Started by
4 comments, last by RobTheBloke 11 years ago

Hello.

I don't even know how to explain this, so here i will try to ask in code.


//I have a class that is like this
class WorkerClass
{

public:
std::vector<Unit> &unit1;
std::vector<Unit> &unit2;
void DoStuff();
void SetUnits(std::vector<Unit> &unit1, std::vector<Unit> &unit2);
};
void WorkerClass::DoStuff()
{
//Change data around in unit1
//Change data around in unit2
}

In my case i got a function "DoStuff()" That dosen't want to care about anything except what is unit1 and unit2.

Problem is that unit1 and unit2 constantly change and they are accessed from:


class Unit
{

public:
//Stuff for unit

};
 
class UnitGroup

{

public:

std::vector<Unit> UnitList;

};
 
//This is what i will have
std::vector<UnitGroup> UnitGroupList;
std::vector<Unit> UnitList;
 
//What i will need to do is
WorkerClass::SetUnits( UnitGroupList[Collided_Unit_Group].UnitList[Some_Unit], UnitList[Some_Unit] );
WorkerClass::DoStuff();
 
//Now UnitGroupList.clear() is called that will clear all data from it.
//Then its populated with unknown amount of units groups, and those unit groups are populated with unknown amount of units
//Then i redo need to call WorkerClass::DoStuff() on new set units
 
WorkerClass::SetUnits( UnitGroupList[Different_Unit_Group].UnitList[Different_Unit], UnitList[Different_Unit] );
WorkerClass::DoStuff();
// ... repeat

Now the problem is the reference will point to old and new unit and produce poop.

Is using a pointer in this case valid? i mean i am pointing to a vector holding a vector and changing its data is that valid? wont i get access violation?

And if so, what should i do? i am out of ideas...

EDIT:: A huge error in "SetUnits" i don't want to set a single unit i want to set the std::vector of units

Advertisement

First, this:


std::vector<Unit> &unit1;
std::vector<Unit> &unit2;
 

should be this:


std::vector<Unit> unit1;
std::vector<Unit> unit2;
 

Second, why is SetUnits taking Unit1 and Unit2 as references and DoStuff has no parameters? They access the Units the same way. Shouldn't it be this?


void DoStuff(Unit &unit1, Unit &unit2);
void SetUnits(Unit &unit1, Unit &unit2); 
 

Beginner in Game Development?  Read here. And read here.

 

I think, that the goal is to have a utility/worker class which accepts two external units which will be proceed later (actually it is quite hard to guess what is the goal of this code smile.png ). Maybe something like this:


//I have a class that is like this
class WorkerClass
{

private:
Unit *unit1;
Unit *unit2;

public:

void DoStuff();
void SetUnits(Unit &unit1, Unit &unit2);
};
void WorkerClass::SetUnits(Unit &pUnit1, Unit &pUnit2)
{
unit1 = &pUnit1;
unit2 = &pUnit2;
}

void WorkerClass::DoStuff()
{
//Change data around in unit1
//Change data around in unit2
}

The set method enforces real objects as parameter (NULL is not allowed), yet the workerclass works internally on pointers only.

First, this:

std::vector<Unit> &unit1;
std::vector<Unit> &unit2;

should be this:

std::vector<Unit> unit1;
std::vector<Unit> unit2;

This would make me at the end force me to do UnitGroupList[Some....].EnemyList[Some...] = unit1, same for other one.

Why do that, when i can and should directly access them.

Second, why is SetUnits taking Unit1 and Unit2 as references and DoStuff has no parameters? They access the Units the same way. Shouldn't it be this?


void DoStuff(Unit &unit1, Unit &unit2);
void SetUnits(Unit &unit1, Unit &unit2); 
 

First of all, the DoStuff() is in span off 5-400 seconds, and because it requires huge(many) amount of stuff, i prefer performance over storage(I understand i am saving on wrong part. That being said, i am exploring more ways on doing things and seeing how they shift structure of code...).

I don't want to pass everything needed, witch each function call, but instead have initalization that will set all its attributes it requires and then let it do whats its meant to do.

Also WorkerClass::SetUnits(std::vector<Unit> &unit1, std::vector<Unit> &unit2); is just getter/setter. with is used to initialize the class before running it.


private:
Unit *unit1;
Unit *unit2;

void WorkerClass::SetUnits(Unit &pUnit1, Unit &pUnit2)
{
unit1 = &pUnit1;
unit2 = &pUnit2;
}

Dosen't &pUnit2; in your WorkerClass::SetUnits function return memory address of the reference?

Or does asking for memory address of reference give memory address of the object its pointing to?

Holy **** i didn't know that asking memory address of reference gives the object its pointing to address.

Thanks that would provide me enough stuff to work with!

EDIT::

void WorkerClass::SetUnits(Unit &pUnit1, Unit &pUnit2) sould be

void WorkerClass::SetUnits(std::vector<Unit> &pUnit1, std::vector<Unit> &pUnit2)

sorry for the error

EDIT2::
That would be it thanks. Fixed with

void WorkerClass::SetUnits(Unit &pUnit1, Unit &pUnit2)
{
unit1 = &pUnit1;
unit2 = &pUnit2;
}

Written a test program this works!

I had no idea that this would work... thanks much!

class WorkerClass
{
public:

WorkerClass(std::vector<Unit>& unit1, std::vector<Unit>& unit2) 
   : m_unit1(unit1), m_unit2(unit2) {}

void DoStuff();
 
private:
  std::vector<Unit>& m_unit1;
  std::vector<Unit>& m_unit2;
};

With references you can't re-assign after construction, so you'd have to construct a new worker class each time. If reassignment is needed, then pointers are the only option.

This topic is closed to new replies.

Advertisement