I'm passing vectors to functions like that

ExtractObjData(MemFile,VectVertex3f,VectFace);
//declaration looks like that.
// void ExtractObjData(char *Mem,vector<Vertex3f> VectVertex3f,vector<int> VectFace);

through ExtractObjData I'm passing this two vectors to.This two func.
//declaration
//void FillVectVertex(string DataStr,vector<Vertex3f> VectVertex3f);
//void FillVectFace(string DataStr,vector<int> VectFace3f);
Like That
ExtractObjData(MemFile,VectVertex3f,VectFace)
{
FillVectVertex(DataStr,VectVertex3f);
FillVectFace(DataStr,VectFace);
}

in this functions I'm filling this vectors with data and in the end of every one of them I'm getting size of it, size is correct.

After returning from function I'm checking size once again, and size is always 0.  :sad: ??
Posted on 2005-06-18 12:56:29 by AceEmbler
You have to pass the vectors by reference.

eg.

void ExtractObjData(char *Mem,vector<Vertex3f> & VectVertex3f,vector<int> & VectFace);
void FillVectVertex(string DataStr,vector<Vertex3f> & VectVertex3f);
void FillVectFace(string DataStr,vector<int> & VectFace3f);

Currently you pass the vectors by value, and any modification you make is lost when you return from the procs.
Posted on 2005-06-18 13:07:18 by chep

You have to pass the vectors by reference.

eg.

void ExtractObjData(char *Mem,vector<Vertex3f> & VectVertex3f,vector<int> & VectFace);
void FillVectVertex(string DataStr,vector<Vertex3f> & VectVertex3f);
void FillVectFace(string DataStr,vector<int> & VectFace3f);

Currently you pass the vectors by value, and any modification you make is lost when you return from the procs.


Thanks, but why it allows me to use Vector benefits than ?? C++ is crazy sometimes.  :P
Posted on 2005-06-18 13:21:21 by AceEmbler
vector benefits? maybe you mean manipulating with data members, like x, y, z, or playing with vector related subroutines?  well, watch how "mov eax, " and "lea eax, " work. This explains all the magic done by C++ when it comes to function parameters :]
Posted on 2005-06-18 13:27:19 by ti_mo_n

Thanks, but why it allows me to use Vector benefits than ?? C++ is crazy sometimes.? :P


I'm not sure understand what you mean.

A reference can be thought of as a pointer, with two notable differences :
- it cannot be uninitialized (eg. cannot be a NULL pointer)
- the syntax remains the same as if it was a "normal" object (ie. you still use the dot operator to access its members, not the -> operator like for pointers).

The reason why your original example doesn't work as you expect is because the vectors are passed "by value", ie. the compiler forces a copy to be made, and the procedures work on this copy rather than on the original object. This is why the original object wasn't updated.
Passing the vectors by reference ensure that the procedures work on the original object rather than on a copy. Nothing else has to be changed in your code, the compiler takes care of everything.
Posted on 2005-06-18 13:51:53 by chep


Thanks, but why it allows me to use Vector benefits than ?? C++ is crazy sometimes.  :P


I'm not sure understand what you mean.

A reference can be thought of as a pointer, with two notable differences :
- it cannot be uninitialized (eg. cannot be a NULL pointer)
- the syntax remains the same as if it was a "normal" object (ie. you still use the dot operator to access its members, not the -> operator like for pointers).

The reason why your original example doesn't work as you expect is because the vectors are passed "by value", ie. the compiler forces a copy to be made, and the procedures work on this copy rather than on the original object. This is why the original object wasn't updated.
Passing the vectors by reference ensure that the procedures work on the original object rather than on a copy. Nothing else has to be changed in your code, the compiler takes care of everything.



I didn't know that this was a copy. It's hard for me to grab all this information about classes.
Posted on 2005-06-18 16:03:39 by AceEmbler