Mar*_*aux 1 c++ valgrind initialization ctor-initializer
我有一个类,只有这样的构造函数:
IntroScreen::IntroScreen(Game *game) :
View(game), counter(0.0f), message(-1), continueAlpha(255),
continueVisible(false), screenAlpha(255), fadeIn(false), fadeOut(false)
{
}
Run Code Online (Sandbox Code Playgroud)
在某个方法的某个地方,我有这个if语句
if (counter > 10.0f)
Run Code Online (Sandbox Code Playgroud)
Valgrind为这条线说:
条件跳转或移动取决于未初始化的值
但我在初始化列表中初始化它!我想我相信Valgrind.因为,有时一切都是正确的,有时没有任何事情发生....所以,可能counter得到一个错误的值,所以它需要很长时间,直到计数器达到10.
我已经检查了我的代码,我使用counter来解决一些错误.但我认为你不能用C++语句"取消初始化一个值"......
这些是我使用的所有行(初始化列表除外)counter:
counter += speed;
counter = 20.0f;
counter += game->getSpeedFactor();
if (counter >= 15.f)
counter = 15.f;
if (counter > 10.0f)
Run Code Online (Sandbox Code Playgroud)
Valgrind给出相同的输出screenAlpha.
这两个变量都是private,我没有friend班级....
那么发生了什么?问题可能是什么?
编辑:
我打印出了值:
在构造函数中,它是correnct:0
在我的方法中,它是垃圾.它引用随机值,如:
-97298.8...-106542.2...print语句是方法的第一行,其中包含所有赋值counter.
这可能是问题!! ??
在我的Game课堂上,我IntroScreen像这样初始化:
Game::Game() : /* Some other stuff .... */ , view(new IntroScreen(this))`
{}
Run Code Online (Sandbox Code Playgroud)
view这里是一个指向抽象超类型被IntroScreen调用的指针View.