defining a public struct inside a class
Hey so i'm making a function that gathers up sprites that you wanna keep on screen, and
then "Check()s" them to make shure they stay, making them bounce of the edge of the screen otherwise.I have defined a struct inside my class ('Object') which holds a refrence to the sprite, and whether it's still outside the screen or not.
(as so the reflect() function isn't called again before the sprite actually returns to being inside the screen)I have three questions regarding the following code:
Will saving a refrence to a sprite in my Object struct allow me to reach the original sprite, and not just a copy when calling reflect()?
Why is the 'Object' Struct underlined in red, saying
Error: implicitly generated constructor for class "OnScreenCheck::Object" cannot initialize.
In the Constructor, i create an Object instance, and pass it on into the Objects vector. I would think since i pass it on to the vector the vector would make a copy of it and save it, but i'm also wondering if the instance would be destroyed after the Constructors done, screwing something up. Will it Work?
Thanks for any help! Here's the Functor:
class OnScreenCheck
{
public:
struct Object{
mySprite& Sprite;
bool OffScreen;};
void AddObject(mySprite& aSprite){
Object newObject;
newObject.Sprite= aSprite;
newObject.OffScreen= false;
Objects.push_back(newObject);
};
void Check(){
// make sure box1 stays on the screen
for(int I=0; I< Objects.size(); I++){
//sprite has gone to high or to low
bool yColl (Objects[I].Sprite.Side(mySprite::top)>=ScreenHeight
|| Objects[I].Sprite.Sid开发者_运维百科e(mySprite::bottom)<=0);
//sprite has gone to far left or right
bool xColl (Objects[I].Sprite.Side(mySprite::left)<=0
|| Objects[I].Sprite.Side(mySprite::right)>ScreenWidth);
if(Objects[I].OffScreen==false){
if(yColl){
Objects[I].OffScreen= true;
Objects[I].Sprite.Reflect(180);}
if(xColl){
Objects[I].OffScreen= true;
Objects[I].Sprite.Reflect(90);}
}
}
};
EDIT: also, my intentions with struct Object are not to have a public data type that belongs to OnScreenCheck, but to make a data type only usable by OnScreenCheck, as it saves these "objects" into the vector Objects. It is simply meant to organize the Data in OnScreenCheck. How can i make it like this, and so i don't have to initialize a Object rightaway in the OnScreenCheck constructor?
Will saving a refrence to a sprite in my Object struct allow me to reach the original sprite, and not just a copy when calling reflect()?
References need to be initialize at the time of creation and they remain bound to the type object they are alias to So Saving a reference to sprite
will always keep pointing to the same sprite
object, any attempt to make it alias to something else will result in an error.
Why is the 'Object' Struct underlined in red, saying Error: implicitly generated constructor for class "OnScreenCheck::Object" cannot initialize.
You need to provide a constructor for OnScreenCheck
which will appropriately initialize Object
member, especially the reference member to sprite
. The error is specifically because references MUST be initialized at time of creation.
In the Constructor, i create an Object instance, and pass it on into the Objects vector. I would think since i pass it on to the vector the vector would make a copy of it and save it, but i'm also wondering if the instance would be destroyed after the Constructors done, screwing something up. Will it Work?
The vector will save a copy of the object and will not be destroyed unless you explicitly ask the vector to do so through vector functions, unless ofcourse your object has some pointer types.
Re the default constructor failure:
struct Object{
mySprite& Sprite;
bool OffScreen;};
When constructed by default - what value do you expect Sprite
to have? It's a reference, it has to reference an existing object, but by default there's none. That's why the error.
So instead you can do this:
class OnScreenCheck
{
public:
struct Object{
Object(mySprite& aSprite, bool screen) : Sprite(aSprite), OffScreen(screen) {};
mySprite& Sprite;
bool OffScreen;};
void AddObject(mySprite& aSprite){
Object newObject(aSprite, false);
Objects.push_back(newObject);
};
addition
Re the addition to your question - the answer is simple: define it as private
or protected
(in case you want to use inheritance in the future). In this way - no code outside of the OnScreenCheck
will be able to access it.
The reference will be valid as long as the original object stays alive (and doesn't move away).
The Object
cannot be default constructed, because the reference must be initialized when created. Assigning to a reference does not change the reference, but the object it refers to.
Storing a copy in the vector and destroying the original seems ok. You probably only want one object around.
精彩评论