Advertisement

How would i pass an array of a class to a function?

Started by March 07, 2003 08:08 AM
35 comments, last by StormUK 21 years, 8 months ago
Title says it all - here is what i have tried so far
  
#include <iostream.h>
#include <windows.h>
#define null 0

class mob
{
public:
	mob()
	{
		posx=0;
		posy=0;
		type=0;
		health=0;
		mana=0;
		ac=0;
		dc=0;
		id=0;
		lastattack=0;
		lastcast=0;
		spellid=0;
		buffid=0;
		castdelay=0;
		attackdelay=0;
	}
	void move(){

	}
	void attack(){
		int now=GetTickCount();
		if(now>=(lastattack+attackdelay))
		{
			cout<<"Attack!"<<endl;
			lastattack=GetTickCount();
		}
	}
	void spell(){
		int now=GetTickCount();
		if(now>=(lastcast+castdelay))
		{
			cout<<"Spell!"<<endl;
			lastattack=GetTickCount();
		}

	}
	void buff(){
		int now=GetTickCount();
		if(now>=(lastcast+castdelay))
		{
			cout<<"Buff!"<<endl;
			lastattack=GetTickCount();
		}
	}

	int posx, posy;
	int type, health, mana, ac, dc, id;
	int spellid, buffid;
	int lastattack, lastcast, castdelay, attackdelay;
};
class spell
{
public:
	spell();
	void effects(){

	}
	spell *next;
private:
	int id;
	int targetid;
	char name[25];
	int duration;
	int tick;
	int range;
	int AC;
	int DC;
	int mana;
	int health;
	int speed;
	int damage;
	int costMP;
	int costHP;
	int level;
	int castTime;
	int radius;
	int cooldown;
	char effect[256];
	char icon[256];
};
class buff
{
public:
	buff();
	void effects(){

	}
	buff *next;
private:
	int id;
	int targetid;
	char name[25];
	int duration;
	int tick;
	int range;
	int AC;
	int DC;
	int mana;
	int health;
	int speed;
	int damage;
	int costMP;
	int costHP;
	int level;
	int castTime;
	int radius;
	int cooldown;
	char effect[256];
	char icon[256];
};

mob mobs(mob monster[]);

int main()
{
	bool done=false;
	int i=0;

	mob monster[10];
	
	//Load Map

	//Load Spells

	//Load Buffs

	//Load Mobs

	//Load Items

	//Load Player


	while(!done)
	{
		monster=mobs(monster, 10);
		
		//Spells

		//Buffs

	}


	return 0;
}

mob mobs(mob monster[], int nummon)
{
	int i=0;
	for(i=0;i<nummon;i++)
	{
		monster[i].attack();
		monster[i].buff();
		monster[i].spell();
	}
				
	return monster;

}
  
Thanks, Chris.
*
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
Advertisement
declare you class to be a friend to access it''s private members

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

and how would i do that?
class A {
friend void B(A&,int);
private:
int var;
}

..
..

void B(A& integer, int number)
{
integer.var=number; //this works now
}


Something like that





The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

gah, i dont 'think' you undersand what i want..

ill try again

Say i had an array and i had a function and i wanted the function to take the array, manipulate the data, then pass it back to the main loop, how would i do this?

could i use the same method if i had an array of a class?

eg;

      class a{int blahvoid somthing() {blah++;}};a somestuff(a myclass);int main(){a myclass[10];myclass=somestuff(myclass);for(int i=0;i<10;i++){cout<<myclass[i].blah<<endl;}return 0;}a somestuff(a myclass){for(int i=0;i<10;i++){myclass[i].somthing}return myclass;}  


[edited by - StormUK on March 7, 2003 10:33:32 AM]
Advertisement
so the array is declared from the main function and
you want to pass the WHOLE array to an outside function and have that data pass back to main??

do something like this...

void ManipData(myclass array[])
{
..the magic happens here
...

}

int main()
{
myclass myarray[10];
ManipData(myarray);
}


myarray holds the address of the myarray[0]

sorry if i still don''t understand your question

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

Thats what im getting at, yeh...how do i return the array to the main loop?
You can just return the array refernce from the paramter. Since you are effectively passing a pointer to the first item in your array, any modification done to the array contents by the function will still be present when the function exits. Passing the array back is needless really.

if you really still want the array back, check this little sample out:


  #include <string>#include <iostream>using namespace std;class myclass {public:	int x;};myclass* func(myclass p[]) {	//magic code of goodness here	return p;};void main() {		myclass array[10];	myclass* arrayptr;	arrayptr = func(array);	for(int i=0;i<10;i++) {		array[i].x = i;	}	cout << arrayptr[1].x << endl;	getchar();}  


Unless I missed the boat one what you are trying to do If so I am sorry.



============================
A guy, some jolt, and a whole lot of code

[edited by - Tyson on March 7, 2003 1:47:06 PM]
============================A guy, some jolt, and a whole lot of code :)
The array is a pointer, when you change the data in another function, then the real original data is changed, you dont need to return it to the main loop

If God with me, Who against me?
--------------------------------------------- If God with me, Who against me?Personal Web Samuel Prince Blog...

This topic is closed to new replies.

Advertisement