[英]Changing a private member variable does not affect the whole class
I have a class, in it is a private
member variable called which is an int
. 我有一个类,其中有一个
private
成员变量,它是一个int
。 For some reason, if I change its value in a method (on the constructor, for example), it will change just fine. 由于某种原因,如果我在某个方法中更改其值(例如,在构造函数中),则它的更改就很好。 But if I change it on a different method and use
printf
to output what its contents are on yet another different method, the value is not carried over and is changed into a very very large number. 但是,如果我用另一种方法更改它,并使用
printf
在另一种方法上输出其内容,则该值不会被保留,而是变成非常大的数字。
Header: 标头:
class Fruit {
private:
int m_fruitState; // 0 = IDLE, 1 = GROWING, 2 = READY, 3 = FALLEN, 4 = ROTTEN
int m_fruitTimer;
public:
Fruit ( );
int getFruitState( ); // Returns m_fruitState
void setFruitState( int fState );
void growFruit( CCTime dt ); // Called every 1 second (CCTime is a cocos2d-x class)
};
CPP CPP
#include "Fruit.h"
Fruit::Fruit( ) {
// Set other member variables
this -> setFruitState( 0 ); // m_fruitState = 0
this -> m_fruitTimer = 0;
this -> m_fruitSprite -> schedule( schedule_selector( Fruit::growFruit ), 1.0 ); // m_fruitSprite is a CCSprite (a cocos2d-x class). This basically calls growFruit() every 1 second
}
int getFruitState( ) {
return this -> m_fruitState;
}
void setFruitState( int state ) {
this -> m_fruitState = state;
}
void growFruit( CCTime dt ) {
this -> m_fruitTimer++;
printf( "%d seconds have elapsed.", m_fruitTimer );
printf( "STATE = %d", this -> m_fruitState ); // Says my m_fruitState is a very big number
// This if condition never becomes true, because at this point, m_fruitState = a very big number
if ( this -> getfruitState( ) == 0 ) { // I even changed this to m_fruitState == 0, still the same
if ( this -> m_fruitTimer == 5 ) { // check if 5 seconds have elapsed
this -> setFruitState( 1 );
this -> m_fruitTimer = 0;
}
}
}
And then on the main, I make an instance of MyClass. 然后在主要方面,我创建MyClass的实例。
I have no idea why that happens. 我不知道为什么会这样。 Why does C++ do that and how do I fix it?
为什么C ++会这样做,我该如何解决? Thanks in advance.
提前致谢。
changeInt( int newInt ); // Assume newInt = 5
Remove the int
from the above line. 从上面的行中删除
int
。
void doSomething( ); {
Remove the ;
删除
;
from the above line. 从上面一行。
Update: Now you're missing a ;
更新:现在您缺少
;
from the end of the header file. 从头文件的末尾开始。 Fixing all the obvious bugs (that would likely keep it from even compiling), it works fine for me.
修复了所有明显的错误(可能会使它甚至无法编译),对我来说很好用。 Either there's still a difference between the code you pasted and the real code, or you've found a compiler bug.
您粘贴的代码与实际代码之间仍然存在差异,或者您发现了编译器错误。
Constructor: myInt = 0
changeInt( int ) : myInt = 5
After constructor and calling changeInt(), myInt = 5
The "selector" argument to schedule
should be a SEL_SCHEDULE
, where 在“选择”参数
schedule
应该是一个SEL_SCHEDULE
,其中
typedef void(CCObject::* SEL_SCHEDULE)(float)
ie it should be a member function of a CCObject
. 即它应该是
CCObject
的成员函数。
It is also supposed to be a member of the object you call schedule
on , otherwise the target when it's called will be wrong. 它也应该是您
schedule
在其上调用的对象的成员,否则目标被调用时将是错误的。
I suspect that this 我怀疑这
this -> m_fruitSprite -> schedule( schedule_selector( Fruit::growFruit ), 1.0 );
causes a call to Fruit::growFruit
with this
pointing at the sprite , not the fruit, which leads to all kinds of unpleasantness. 导致通话
Fruit::growFruit
与this
指着精灵 ,而不是水果,这会导致各种不愉快的。
(Note that schedule_selector
does a C-style cast, which means that it's inherently unsafe. Don't use it.) (请注意,
schedule_selector
执行C样式强制转换,这意味着它本质上是不安全的。请勿使用它。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.