Using class object member variables based on other members

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Using class object member variables based on other members

Post by TheThirdL3g »

I need to search through a class to find the object who's "id" member variable matches a value. This class object's other member variables will be used for a calculation, but only if the "id" equal.

This concept code might help in understanding what I'm trying to do:

Code: Select all

class testClass {
	public:
		int id:
		int diff;
		};
		
void function(int fId) {
	/*
	Checks which object has the member variable "id" equal to the "fId"
	and uses this objects other member variables for a calculation.
	For example lets say there are two objects for testClass. Object one has a diff of 1 and an id of 10. 
	Object two has a diff of 2 and an id of 11. If fId = 10 then the diff value of object one will be used.
	*/
	}
What I tried defiantly didn't work so any help would be really appreciated.
Last edited by TheThirdL3g on Sun Apr 04, 2010 11:12 am, edited 1 time in total.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Using class object member variables based on other member it

Post by XianForce »

TheThirdL3g wrote:I need to search through a class to find the object who's "id" member variable matches a value. This class object's other member variables will be used for a calculation, but only if the "id" equal.

This concept code might help in understanding what I'm trying to do:

Code: Select all

class testClass {
	public:
		int id:
		int diff;
		};
		
void function(int fId) {
	/*
	Checks which object has the member variable "id" equal to the "fId"
	and uses this objects other member variables for a calculation.
	For example lets say there are two objects for testClass. Object one has a diff of 1 and an id of 10. 
	Object two has a diff of 2 and an id of 11. If fId = 10 then the diff value of object one will be used.
	*/
	}
What I tried defiantly didn't work so any help would be really appreciated.
Well I'm going to assume that every object has a unique id, and if so, you could create a map of integers and objects, and then loop through them, checking to see which one contains the desired ID.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Using class object member variables based on other member it

Post by Falco Girgis »

XianForce wrote:
TheThirdL3g wrote:I need to search through a class to find the object who's "id" member variable matches a value. This class object's other member variables will be used for a calculation, but only if the "id" equal.

This concept code might help in understanding what I'm trying to do:

Code: Select all

class testClass {
	public:
		int id:
		int diff;
		};
		
void function(int fId) {
	/*
	Checks which object has the member variable "id" equal to the "fId"
	and uses this objects other member variables for a calculation.
	For example lets say there are two objects for testClass. Object one has a diff of 1 and an id of 10. 
	Object two has a diff of 2 and an id of 11. If fId = 10 then the diff value of object one will be used.
	*/
	}
What I tried defiantly didn't work so any help would be really appreciated.
Well I'm going to assume that every object has a unique id, and if so, you could create a map of integers and objects, and then loop through them, checking to see which one contains the desired ID.
I wouldn't recommend a map, since it's pretty resource heavy and your objects already have a unique ID (so why associate it outside of itself?).

I would either have a vector/list of all of your objects then iterate through comparing their IDs with the passed fID, OR have a simple array of the objects where their indices directly correspond to their IDs. This is the preferred method, but I'm not sure where your unique IDs are coming from.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: Using class object member variables based on other member it

Post by TheThirdL3g »

Thanks for the help.
GyroVorbis wrote:This is the preferred method, but I'm not sure where your unique IDs are coming from.
I probably should of put more into the example code. This should explain the id's a little better.

Code: Select all

class testClass {
   public:
      int id;
      int diff;
      static size_t count;
      testClass(int);
      };

size_t armor::count = 0;

testClass::testClass() {
	count++;
	id = 100 + count;
}

User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Using class object member variables based on other members

Post by zeid »

The code you provide doesnt really show an attempt at the issue. You could put together a simple entity manager class that encapsulates these things:
Also depending on your needs it might be worth making the entity manager a singleton (I will also provide code for a singleton template)
Note I haven't tested this code, so not sure if there are any syntax errors.

Code: Select all

#ifndef _ENTITYMANAGER_H
#define _ENTITYMANAGER_H

#include "Entity.h"
#include <list>
class EntityManager : public Singleton<EntityManager>
{
  friend class Singleton<EntityManager>;
public:
  template<typename T>
  T *create()
  {
     T *e=new T;
     entityList.push_back(e);
     return e;
  }

  Entity *findId(int p_id)
  {
    for(list<Entity*> iter=entityList.begin; iter!=entityList.end; ++iter)
    {
       Entity* e=*iter;
       if(e->id==p_id)
          return e;
    }
    return NULL;
  }

  void destroy(Entity *p_e)
  {
    entityList.remove(p_e);
    delete p_e;
  }
private:
  list<Entity*> entityList;
  EntityManager(){}
  ~EntityManager()
   {
      for(list<Entity*> iter=entityList.begin; iter!=entityList.end; ++iter)
      {
         Entity* e=*iter;
         delete e;
      }
      entityList.clear();
   }
};
#endif
Heres the singleton class I use.

Code: Select all

#ifndef _SINGLETON_H
#define _SINGLETON_H

template<typename T>
class Singleton
{
public:
	static T *instance()
	{
		if(m_singleton==0)
			m_singleton=new T();
		return m_singleton;
	}
	
	void static release()
	{
		delete m_singleton;
		m_singleton = 0;
	}
	
protected:
	Singleton(){}
	virtual ~Singleton(){}
	
private:
	static T *m_singleton;
	Singleton(const Singleton &s);	
};
template<typename T> T *Singleton<T>::m_singleton=0;

#endif
Advantages of this approach...

* You don't have to worry about memory leaks as much as this structure ensures all entities are deleted and the end of the program.
* You can easily itterate through all entities by adding a loop similar to that in findId()
* You can add derived classes of Entity to the entity list, as the create() method is a template function. Hence you could do a call such as entityManager->create<Entity>(); or entityManager->create<DerivedEntity>();
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
vargonian
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 60
Joined: Fri Aug 07, 2009 3:34 pm

Re: Using class object member variables based on other members

Post by vargonian »

I call homework assigment on this one.

But maybe not.

Just use:

Code: Select all

var matchingObjects = from object in myObjects where object.id == fId select object;
Oh right, this isn't C#. But since this is likely a homework assignment, the teacher is presumably just looking for you to demonstrate an understanding of looping through objects, hence using something like a map will be a red flag for "copied off the internet". What you want is just something like:

Code: Select all

for (int i = 0; i < numObjects; i++)
{
  MyObjectType* object = myObjects[i]; // this assumes it's an array of *pointers* to objects
  if (object.id == fId)
  {
    // do something
    break; // if there's the potential for more than one match, remove this break.
  }
}
(Apologies if this really isn't a homework assignment. In that case, I would probably use something other than just a simple loop, depending on what you're using this for.)
Image
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Using class object member variables based on other members

Post by avansc »

binary tree on the ID?

alternatively, and ALOT easier since you dont have to balance it, is sort the array of classes, then just do a binary search on that. should get you some extra points.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Using class object member variables based on other members

Post by avansc »

Here is another way of looking at it, not really resource friendly, but a really really good exercise for sorting out things like conditional logic, loops, arrays, and pointers.

Code: Select all


#include <iostream>

using namespace std;

class id
{
public:
	id(int id);
	int i_Id;
};

id::id(int id)
{
	this->i_Id = id;
}

class node
{
public:
	node();
	node(id *data);
	
	node *children[10];
	id *data;
};

node::node()
{
	this->data = NULL;
	for(int a = 0;a < 10;a++)
		this->children[a] = NULL;
}

node::node(id *data)
{
	this->data = data;
	for(int a = 0;a < 10;a++)
		this->children[a] = NULL;
}

void addId(node *root, id *data)
{
	node *temp = root;
	int tid = data->i_Id;
	while(tid > 0)
	{
		if(temp->children[tid%10] == NULL)
			temp->children[tid%10] = new node();
		
		temp = temp->children[tid%10];
		tid /= 10;
	}
	temp->data = data;
}

node *getId(node *root, int id)
{
	node *temp = root;
	while (id > 0)
	{
		if(temp->children[id%10] == NULL)
			return NULL;
		temp = temp->children[id%10];
		id /= 10;
	}
	return temp;
}

int main(int argc, char * const argv[])
{
	node *root = new node();
	addId(root, new id(2345));
	addId(root, new id(2331));
	if(node *temp = getId(root, 2331))
		cout << temp->data->i_Id << endl;
	else
		cout << "No such ID\n";

	return 0;
}

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply