Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 建造商';初始化指针数据成员时出现问题_C++_Class_Pointers_Constructor - Fatal编程技术网

C++ 建造商';初始化指针数据成员时出现问题

C++ 建造商';初始化指针数据成员时出现问题,c++,class,pointers,constructor,C++,Class,Pointers,Constructor,初始化指针数据成员(即int*apex)时遇到问题;构造函数内部 参数为int i=0;as*apex=i; 但不幸的是,在编译器到达这一行之后,什么也不执行 #include <iostream> using namespace std; class base{ int *apex; public: explicit base(int i = 0){ cout << "this does executes"

初始化指针数据成员(即int*apex)时遇到问题;构造函数内部 参数为int i=0;as*apex=i; 但不幸的是,在编译器到达这一行之后,什么也不执行

#include <iostream>
using namespace std;

class base{
    int *apex;      
public:
    explicit base(int i = 0){
        cout << "this does executes" << endl;
        *apex = i; // <<<<<--- problem???
        cout << "this doesnt executes" << endl;
    }
};

int main(void){
    base test_object(7);
    cout << "this also doesnt executes";
}


// I know how to avoid this but i want to know what
// exactly the problem is associated with *apex = i;
#包括
使用名称空间std;
阶级基础{
int*顶点;
公众:
显式基(int i=0){

cout您的指针指向无效地址—您没有初始化它 这将修复您要求执行的操作

using namespace std;

class base{
    int *apex{nullptr};      
public:
    explicit base(int& i ): apex{&i} {
        cout << "this does executes" << endl;
        cout << "this doesnt executes" << endl;
    }
};

int main(void){
    int a = 7
    base test_object(a);
    cout << "this also doesnt executes";
}
使用名称空间std;
阶级基础{
int*apex{nullptr};
公众:
显式基(int&i):顶点{&i}{

cout您的指针指向无效地址—您没有初始化它 这将修复您要求执行的操作

using namespace std;

class base{
    int *apex{nullptr};      
public:
    explicit base(int& i ): apex{&i} {
        cout << "this does executes" << endl;
        cout << "this doesnt executes" << endl;
    }
};

int main(void){
    int a = 7
    base test_object(a);
    cout << "this also doesnt executes";
}
使用名称空间std;
阶级基础{
int*apex{nullptr};
公众:
显式基(int&i):顶点{&i}{

cout你写的内容相当于:

int *apex;
*apex = 42;
这就是undefined behavior(UB),其中包括编译器可能只包含停止执行或开始播放Rick Astley的歌曲永不放弃的代码

甚至

将是UB,因为通过
*
取消引用时,
int*
指针必须指向有效的
int

只要写

class base{
    int apex{};      
public:
    explicit base(int i) : apex(i){}
};

你写的东西相当于:

int *apex;
*apex = 42;
这就是undefined behavior(UB),其中包括编译器可能只包含停止执行或开始播放Rick Astley的歌曲永不放弃的代码

甚至

将是UB,因为通过
*
取消引用时,
int*
指针必须指向有效的
int

只要写

class base{
    int apex{};      
public:
    explicit base(int i) : apex(i){}
};

我明白了。相信我,在这愚蠢的怀疑之后,我为自己感到羞愧

#include <iostream>
using namespace std;

class base{
    int *apex;      
public:
    explicit base(int i = 0){
        apex = new int;
        // this is what i was supposed to do
        *apex = i;
    }
};

int main(void){
    base test_object(7);
}
#包括
使用名称空间std;
阶级基础{
int*顶点;
公众:
显式基(int i=0){
顶点=新的整数;
//这就是我应该做的
*顶点=i;
}
};
内部主(空){
基本测试对象(7);
}

我明白了。相信我,在这愚蠢的怀疑之后,我为自己感到羞愧

#include <iostream>
using namespace std;

class base{
    int *apex;      
public:
    explicit base(int i = 0){
        apex = new int;
        // this is what i was supposed to do
        *apex = i;
    }
};

int main(void){
    base test_object(7);
}
#包括
使用名称空间std;
阶级基础{
int*顶点;
公众:
显式基(int i=0){
顶点=新的整数;
//这就是我应该做的
*顶点=i;
}
};
内部主(空){
基本测试对象(7);
}


您从不初始化指向有效内存的指针。因此,
*apex
调用未定义的行为这与构造函数或类无关。它与取消引用未初始化的指针有关。这是否回答了您的问题?您只能初始化构造函数的初始化器列表中的成员变量。Wh你现在做的是赋值,而不是赋值给指针,而是赋值给一些不存在的东西。你从不初始化指针指向有效内存。因此
*apex
调用未定义的行为这与构造函数或类无关。它与取消引用未初始化的指针有关。这是否回答了问题你的问题?你只能在构造函数的初始值设定项列表中初始化成员变量。你所做的是赋值,而不是赋值给指针,而是赋值给一些不存在的东西。不。这是错误的。UB潜伏在角落里,因为
apex
存储了一个悬空的指针。
i
是构造函数的本地对象现在it不同,但仍然是错误的。“确保给定给ctor的对象比实例具有更长的生存期”在你的例子中,你没有这样做。指针仍然无效。哦,不,实际上这没有编译。虽然这没有错,但我对这个问题的解释是不同的。看起来OP在编写
*apex=i;
时想要的是值而不是指针,当然我可能错了。我只考虑了问题的这一部分。“我在初始化指针数据成员时遇到问题”否。这是错误的。UB潜伏在拐角处,因为
apex
存储了一个悬空的指针。
I
是构造函数的本地对象。现在它不同了,但仍然是错误的。“确保给定给ctor的对象的生存期比实例长”在你的例子中,你没有这样做。指针仍然无效。哦,不,实际上这没有编译。虽然这没有错,但我对这个问题的解释是不同的。看起来OP在编写
*apex=i;
时想要的是值而不是指针,当然我可能错了。我只考虑了问题的这一部分。“我在初始化指针数据成员时遇到问题”是,或
explicit base(int I=0):如果
apex
出于某种原因确实需要成为指针,则apex{new int(I)}{code>。通常情况下,不应该在
std::unique\u ptr
中使用
std::make_unique(I)
那么,否则这在很大程度上取决于它必须是指针的原因。是的,或者
显式基(inti=0):如果
apex
出于某种原因确实需要是指针,那么apex{new int(i)}
apex{new int(i)}
那么,否则这在很大程度上取决于它必须是指针的原因。