Will these Variables be changed?

Started by
13 comments, last by Esap1 23 years, 9 months ago
as for passing a list of classes (i''m assuming it''s an array you mean) you''re changer function should work fine:

    class Class_of_Death;void ChangeStuff(Class_of_Death *classlist) {// changes ''someValue'' in the 2nd index in the classlist arrayclasslist[2].someValue = 100;// does the exact same thing but on the 3rd index of classlist(classlist+3)->someValue = 200;}// Declare a list either of these ways, it''s basically the sameClass_of_Death CoD[5];Class_of_Death *Cod = new Class_of_Death[5];ChangeStuff(CoD);  // Passes pointer to beginning of arrayChangeStuff(&CoD[0]); // Passes pointer to first index in arrayChangeStuff( (CoD+0) );    



man i''m bored.. i think that''s all correct
-werdup-
Advertisement
I believe that if you used a pointer to a pointer that your function would be able to permanently change the variables.
---Ranok---
Ok, I got the int's working. Now, the pointer to an Array of class's that Im sending to the function is zero. Then the function uses new.
like this:

CFace *Face;
Face=0;
LoadStuff(Face); //How should I pass it?
//Face now should have all the Faces in it

//Other File

void LoadStuff(CFace Face); //How do I do it?
{
//find num face
Face = new CFace[numFace];
//load face
}


How should I do it so that it works like that?

Edited by - Esap1 on July 24, 2000 6:13:58 PM
Basically I need a function that can exept a Null CFace pointer, then use, thepointer = new CFace[numFace], fill it up, and when the function returns, thepointer points to the faces, HOW?
Is there some reason you don''t want the class to parse/read the file?

short answer to have a function change one of it''s params
it has to know where what you want changed lives, if what you want changed is a pointer then you want a pointer to pointer

void foo(int **a,int count) {
*a = new int[count];
(*a)[3] = 10;
}

int *a=0;
foo(a,5);
printf("%d\n",a[3]);

yields "3";

Note that if *a actually pointed to something YOU HAVE NOW LEAKED THAT MEMORY!

The OOP way of doing this would be something like this (forive my bad htmlisms, I don''t know how you qoute code here)

class CMap {
unsigned numVertex;
unsigned numFace;
vector < CVertex > Vertex;
vector < CFace > Face;
// shouldn''t tu tv be members of Face?
public:
// construct Vertex and Face as having 0 size as we don''t
// know how big to make them yet
CMap() : numVertex(0),numFace(0),Vertex(0),Face(0) {}
};


bool CMap::ReadFromStream(istream &in) {
in >> numVertex;
if(in.bad()) return false;
in >> numFace;
if(in.bad()) return false;
/* The file will now have numVerticies as 3 floats I assume
we hand CVertex the input stream and let it load itself
doing things like this is kinda the whole point
*/
Vertex.reserve(numVertex); // This allocates the space
for(int i=0;i if(!Vertex.ReadFromStream(in)) return false;
}
Face.reserve(numFace); // This allocates the space
for(int i=0;i if(!Face.ReadFromStream(in)) return false;<br> }<br> return true;<br>}<br><br>Now as long as Vertex and Face have ReadFromStream methods that<br>understand thier own datatypes and return false if they encouter bad data you are done.<br><br>init your object like so<br><br>CMap map;<br>if(!map.ReadFromStream(ifstream(filename))) cerr &lt&lt "oops";<br><br>The above can also be changed to use binary files fairly easily, its just often best to start of reading from files that you can edit/create easily in a text editor.<br><br> </i>

This topic is closed to new replies.

Advertisement