• Advertisement
Sign in to follow this  

quick question in c++ - vector

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey.. working on my first projekt, card game. anyways, question is: if i declare a global vector, must I pass it do a function, or can i access it from the function without passing it to the function? 2nd thought - can one make a local vector?! arent they allways global?. anyways, do i have to pass it to a function to access it? c++ dev-c++ 4.0 thx guys! - niux

Share this post


Link to post
Share on other sites
Advertisement
You can access global vector from everywhere, you don't have to pass it to a function.
A global vector can only be accessed by passing it to a function, but you have to make sure, that the vector still exists, because local objects are deleted when there scope is left.




std::vector< int > globalVec;

void func()
{
globalVec.push_back( 12 );
}

int main()
{
globalVec.push_back( 3 );
func()
cout << globalVec[ 0 ] << " " globalVec[ 1 ] << endl; // print both elements
}


For local usage you can do something like that


void func( std::vector< int >& vec )
{
vec.push_back( 12 );
}

int main()
{
std::vector< int > localVec;
localVec.push_back( 3 );
func( localVec );
cout << localVec[ 0 ] << " " << localVec[ 1 ] << endl; // print both elements
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Niux
hey.. working on my first projekt, card game.

anyways, question is: if i declare a global vector, must I pass it do a function, or can i access it from the function without passing it to the function?

2nd thought - can one make a local vector?! arent they allways global?. anyways, do i have to pass it to a function to access it?


In this regard, there is absolutely nothing special about vectors. A global variable can be accessed "anywhere, without passing it in", regardless of its type. A vector can be declared as a local variable, just like you can have a local variable of any other type.


int globalInt;
vector<int> globalIntVector;

void doSomethingStupid(int paramInt, vector<int>& paramIntVector) {
int localInt;
vector<int> localIntVector;
localInt = 3; localIntVector.push_back(localInt);
globalInt = 3; globalIntVector.push_back(globalInt);
// All works just fine.
paramIntVector.push_back(paramInt);
// Since the vector was passed in by reference, the change will be "seen"
// by the caller.
// And of course, you can mix and match too; there are no restrictions on
// inter-operation posed by the variable scopes - they're all in scope here:
paramIntVector.push_back(localInt); // for example, etc.
// At end of function, the local int just falls off the stack, and the
// destructor for the local int vector is called, which frees up its storage
// space.
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement