Sign in to follow this  
Followers 0
pressgreen

Scope MADNESS

4 posts in this topic

Ok so I want to know if i were to set the following function to run when i press a key on the keyboard, would it run and stack the object in the vector? and would I be able to access it outside the function afterwards. also would the newObject be usable if i was pushing it on the stack one after another after i keep pressing the key that runs the function. I am trying to create a stack of objects that are unique so I can have several different object i create every time i press the key. so i need to know how does the scope work for vectors and class objects in this manor. could some one please explain how this works or how i might approach this problem.

 

 

///pseudo code

 

vector<CoolClass> ObjectStack;

 

void function(){

 

CoolClass  newObject;

 

ObjectStack.push_back(newObject);

 

}

 

 

ObjectStack[0].DoStuffWithObject();

0

Share this post


Link to post
Share on other sites
This depends on the exact scope of the vector and the implementation of CoolClass. CoolClass needs to be properly copyable (ex: obeying the rule of three if applicable) and the vector in an appropriate scope to be accessible both from the function that wants to push objects into the vector and the function that accesses the vector later.
3

Share this post


Link to post
Share on other sites

The vector would be declared static. what i want to know though is lets say I hit the key that runs that function 6 times. will there be 6 different versions of newObject in the stack? or would it be like all six elements in that stack pointing to the same object? 

0

Share this post


Link to post
Share on other sites

The vector would be declared static. what i want to know though is lets say I hit the key that runs that function 6 times. will there be 6 different versions of newObject in the stack? or would it be like all six elements in that stack pointing to the same object? 

6 different versions.

 

If written exactly like this, your scope works.  You create a new instance of CoolClass each time the function runs.  That instance is copied when you push it to the vector, which is why SiCrane mentioned the need to be properly copyable (depending on the details of CoolClass... if it's a POD type, with no pointer funny business, a bit-wise default copy will do the job).

 

When the function returns, the "new instance" that the newObject variable represents is removed from the stack, but the copy of that instance, itself a complete instance of CoolClass, is still in the vector.

 

So as many times as you run the function, that many CoolClass objects will be in the vector.

Edited by BCullis
2

Share this post


Link to post
Share on other sites

I've been doing C++ on and off for several years but I'd never heard of the Rule of Three so I looked it up and figured I'd drop the link here for reference. Thanks SiCrane.

 

@OP: What BCullis said. 

 

Haha I'd started to write more or less exactly that, but you beat me to it.

Edited by Jutaris
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0