Jump to content

  • Log In with Google      Sign In   
  • Create Account


Scope MADNESS


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 17 March 2013 - 04:07 PM

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

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9328

Like
3Likes
Like

Posted 17 March 2013 - 04:38 PM

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 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 18 March 2013 - 07:56 AM

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

#4 BCullis   Crossbones+   -  Reputation: 1813

Like
2Likes
Like

Posted 18 March 2013 - 08:05 AM

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, 18 March 2013 - 08:06 AM.

Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#5 Jutaris   Members   -  Reputation: 473

Like
1Likes
Like

Posted 18 March 2013 - 08:07 AM

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, 18 March 2013 - 08:11 AM.

My Site -- My Music  -- My Ramblings -- My Game

 Basic Flying Rules:Try to stay in the middle of the air. Do not go near the edges of it. The edges of the air can be recognized by the appearance of ground, buildings, sea, trees and interstellar space. It is much more difficult to fly there.

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS