Scope MADNESS

Started by
3 comments, last by FalconDragoon 11 years, 1 month ago

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();

J-GREEN

Greenpanoply
Advertisement
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.

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?

J-GREEN

Greenpanoply

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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.

My Games -- My Music 

Come join us for some friendly game dev discussions over in XNA Chat!

This topic is closed to new replies.

Advertisement