我使用初始化列表进行了以下几次尝试,但我从来没有能够很好地解释它.任何人都可以解释为什么以下失败(我没有编译器来捕捉拼写错误,所以请耐心等待):
class Foo
{
public:
Foo( int i ) : m_i( i ) {} //works with no problem
int getInt() {return m_i;}
~Foo() {}
private:
int m_i;
};
class Bar
{
public:
Bar() :
m_foo( 5 ), //this is ok
m_myInt( m_foo.getInt() ) //runtime error, seg 11
{}
~Bar() {}
private:
Foo m_foo;
int m_myInt;
};
Run Code Online (Sandbox Code Playgroud)
当尝试调用初始化列表初始化的成员的成员函数时,我得到seg错误.我似乎记得这是一个已知的问题(或者可能以某种方式设计),但我从未见过它.附加的示例是使用普通的旧数据类型设计的,但Bar::m_myInt用另一个缺少默认(空)构造函数的对象替换,问题更加真实.任何人都可以开导我吗?
我很难调试生产崩溃.只是想与这里的人们确认语义.我们有一个类......
class Test {
public:
Test()
{
// members initialized ...
m_str = m_str;
}
~Test() {}
private:
// other members ...
std::string m_str;
};
Run Code Online (Sandbox Code Playgroud)
有人改变了初始化以使用ctor初始化列表,这在我们的代码语义中是合理正确的.初始化顺序及其初始值是正确的.所以班级看起来像......
class Test {
public:
Test()
: /*other inits ,,, */ m_str(m_str)
{
}
~Test() {}
private:
// other members ...
std::string m_str;
};
Run Code Online (Sandbox Code Playgroud)
但是代码突然崩溃了!我在这段代码中分隔了很长的列表m_str(m_str).我通过链接文本证实了这一点.
它必须崩溃吗?标准对此有何看法?(这是不确定的行为吗?)
可能重复:
C++构造函数名后的冒号是做什么的?
我正在读一本关于CUDA的书,我在阅读这个C++语法时遇到了麻烦.我不知道该搜索什么,这就是我在这里发帖的原因.
struct cuComplex {
float r;
float i;
cuComplex( float a, float b ) : r(a) , i(b) {}
}
Run Code Online (Sandbox Code Playgroud)
该cuComplex声明的作用是什么?特别:
cuComplex( float a, float b ) : r(a) , i(b) {}
Run Code Online (Sandbox Code Playgroud)
这叫什么,所以我可以了解它?
我有一个struct A有几个构造函数,初始化不同的数据成员.
template<typename T>
struct A {
typedef std::vector<T> type1
type1 a;
type1 b;
type1 c;
A(type1 i_a): a(i_a) {
}
A(type1 i_a, type1 i_b): A(i_a), b(i_b) {
}
A(type1 i_a, type1 i_b, type1 i_c): A(i_a, i_b), c(i_c) {
}
};
Run Code Online (Sandbox Code Playgroud)
我得到的错误是当我用它实例化它时custom_type,错误是
type A<custom_type> is not direct base of A<custom_type>突出显示我在另一个构造函数中调用的构造函数.我正在使用C++ 11.有什么问题?
我知道不应该在构造函数中直接或间接调用虚函数,但是这段代码运行正常.
我在这里安全吗?
#include <iostream>
#include <string>
struct A {
A (const std::string& name) {std::cout << name << std::endl;}
virtual std::string tag() const = 0;
};
struct B: A {
B() : A (tag()) {}
virtual std::string tag() const override {return "B";}
};
int main() {
B b; // Output gives "B\n"
}
Run Code Online (Sandbox Code Playgroud)
如果没有,以下(基于评论)是否是正确的解决方法?
// Replacement for class B:
struct B: A {
B() : A (name()) {}
virtual std::string tag() const override {return name();}
private:
static std::string name() {return "B";} …Run Code Online (Sandbox Code Playgroud) 我有一个类,只有这样的构造函数:
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 …Run Code Online (Sandbox Code Playgroud) #include<iostream>
using namespace std;
class A
{
public:
int i;
A() {cout<<"A()"<<endl;}
~A() {cout<<"~A()"<<endl;}
};
class B:public A
{
public:
int j;
B(): j(10)
{
this->i=20;
this->~A();
}
};
int main()
{
B abc;
cout<<"i="<<abc.i<<" j="<<abc.j<<endl;
}//main
Run Code Online (Sandbox Code Playgroud)
两个问题:
有没有办法在B的构造函数中为A创建一个初始化列表?像这样的东西:
class B:public A
{
B(): j(10), A():i(20) {}
};
Run Code Online (Sandbox Code Playgroud)可能重复:
C++构造函数名后的冒号是做什么的?
我发现这个语法在C++中很奇怪
TagDetails::TagDetails(QWidget *parent) :
QDialog(parent),
ui(new Ui::TagDetails)
Run Code Online (Sandbox Code Playgroud)
这是C++中构造函数的声明......冒号后的东西代表什么,即ui(new Ui :: TagDetails)在这里是什么意思?结肠是什么?
我正在学习C++.只是好奇,只能在类声明中为静态和常量变量赋值吗?这主要是为什么当您为正常成员分配值时,他们有一种特殊的方式来做它
void myClass::Init() : member1(0), member2(1)
{
}
Run Code Online (Sandbox Code Playgroud)