Archived

This topic is now archived and is closed to further replies.

Esap1

Will these Variables be changed?

Recommended Posts

Esap1    122
If I had a function:
    
int LoadFullASE(char *filename, BOOL ShouldLoadTextures,CTextureManager *TextureManager,unsigned int numVertex,unsigned int numFace,	CVertex *Vertex, CFace *Face, float *tu, float *tv);

class CMap
{
public:
unsigned int numVertex;
unsigned int numFace;
CVertex *Vertex;
CFace *Face;
float *tu;
float *tv;
void LoadFile(char *file);
}

void CMap::Loadfile(char *file)
{
LoadFullASE(filename,1,&TextureManager,numVertex,numFace,Vertex,Face,tu,tv);
}
    
Would those Variables in CMap, get changed. Im trying to make them be able to changed by LoadFullASE. Know How?? Thanks yall, later, [source]

Share this post


Link to post
Share on other sites
Zipster    2365
Yes they will be changed if the LoadFullASE function actually changes them, but you are passing the class''s variables through the function.

=======================================
A man with no head is still a man.
A head with no man is plain freaky.

Share this post


Link to post
Share on other sites
Esap1    122
Are you sure, exactly how I wrote it, if The function is changing those Variables, then they will be changed Forever?. It seems that when the function ends, that the variables are the same, 0, even though the function clearly sets them, I dont get it?

Share this post


Link to post
Share on other sites
Prosper/LOADED    100
                
int LoadFullASE(char *filename, BOOL ShouldLoadTextures,CTextureManager *TextureManager,unsigned int numVertex,unsigned int numFace, CVertex *Vertex, CFace *Face, float *tu, float *tv);

[/source]


No. They won't be changed. You'll be able to change the things POINTED by the pointers, but not the pointers themself.

If you want to change them, just add the & operator before each argument you want to change.

If you do :

[source]
void function( int & a, int b)
{
a = 1;
b = 1;
}

void main()
{
int a = 0;
int b = 0;

function( a, b);

cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
}


You'll get :

a:1
b:0


Edited by - Prosper/LOADED on July 23, 2000 11:33:36 PM

Edited by - Prosper/LOADED on July 23, 2000 11:34:34 PM

Share this post


Link to post
Share on other sites
Esap1    122
When I put the & sign, I get a million errors. Whats up. I get errors on the, Face, Vertex, tu, tv. All the ones were they are an Array. How could I change:
void Changer(float *hi)
{
hi[0] = 34;
hi[1] = 32;
}

How would I changed that function and call that function so that hi gets changed?

PS: What the hell does the & sign actually do?

Share this post


Link to post
Share on other sites
Esap1    122
The code works for an int value, but what about a pointer to a list of class''s?, how would I do it?

Share this post


Link to post
Share on other sites
shmaLbus    122
um, of course they won''t be changed in your function, when you pass them like...

void blah(int a);

that''s passing the value of whatever

int bigBadInteger=1;
blah(bigBadInteger);

''a'' in blah is going to equal one and has no connection to bigBadInteger.
    
Use..
void blah(int &a);
blah(bigBadInteger);

OR..
void blah(int *a);
blah(&bigBadInteger);



with they way you''re doing, the pointers you''re passing should change though. just not numVertex and numFace unless i misread.

Share this post


Link to post
Share on other sites
shmaLbus    122
btw.. the '&' sign passes a reference or in other words it passes the address of the variable (a pointer to it if that helps it make any more sense).

if you have:
int a;
where address of a == 00000000 (where it's stored in mem)
and the value of a == 10 (the value the variable holds)

a == 10
&a == 00000000
get it?

Edited by - shmaLbus on July 24, 2000 10:57:23 AM

Share this post


Link to post
Share on other sites
shmaLbus    122
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 array

classlist[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 same
Class_of_Death CoD[5];
Class_of_Death *Cod = new Class_of_Death[5];

ChangeStuff(CoD); // Passes pointer to beginning of array

ChangeStuff(&CoD[0]); // Passes pointer to first index in array

ChangeStuff( (CoD+0) );



man i''m bored.. i think that''s all correct

Share this post


Link to post
Share on other sites
Ranok    122
I believe that if you used a pointer to a pointer that your function would be able to permanently change the variables.

Share this post


Link to post
Share on other sites
Esap1    122
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

Share this post


Link to post
Share on other sites
Esap1    122
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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[i].ReadFromStream(in)) return false;
}
return true;
}

Now as long as Vertex and Face have ReadFromStream methods that
understand thier own datatypes and return false if they encouter bad data you are done.

init your object like so

CMap map;
if(!map.ReadFromStream(ifstream(filename))) cerr << "oops";

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.

Share this post


Link to post
Share on other sites