Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1246

Like
1Likes
Like

Posted 03 April 2013 - 01:37 AM

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


Edited by BaneTrapper, 03 April 2013 - 03:06 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 03 April 2013 - 02:07 AM

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.
 
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
 
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts Posted Image
 
Spoiler

#3 Ashaman73   Crossbones+   -  Reputation: 7991

Like
2Likes
Like

Posted 03 April 2013 - 02:33 AM

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.


Edited by Ashaman73, 03 April 2013 - 02:36 AM.


#4 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 03 April 2013 - 02:38 AM

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.


Edited by BaneTrapper, 03 April 2013 - 02:40 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#5 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 03 April 2013 - 02:51 AM

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!


Edited by BaneTrapper, 03 April 2013 - 03:05 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#6 RobTheBloke   Crossbones+   -  Reputation: 2341

Like
1Likes
Like

Posted 03 April 2013 - 05:01 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS