Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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++_Arrays_Pointers - Fatal编程技术网

C++ 使用指针访问数组元素时出现意外结果

C++ 使用指针访问数组元素时出现意外结果,c++,arrays,pointers,C++,Arrays,Pointers,我有两节课类别1和class2class2使用class1类型的元素创建数组class2有一个指向该数组的成员。我可以使用class 2的getpointer方法访问指针,但是当我尝试使用指针访问数组元素的成员时,会得到意外的结果 代码如下: #include <iostream> using namespace std; class class1{ int a; int b; public: class1(){} class1(int x, int

我有两节课<代码>类别1和
class2
class2
使用
class1
类型的元素创建数组
class2
有一个指向该数组的成员。我可以使用
class 2的
getpointer
方法访问指针,但是当我尝试使用指针访问数组元素的成员时,会得到意外的结果

代码如下:

#include <iostream>
using namespace std;

class class1{
    int a;
    int b;
public:
    class1(){}
    class1(int x, int y){a = x; b = y;}
    int geta(){return a;}
    int getb(){return b;}

};

class class2{
    int c;
    int d;
    class1 *e;
public:
    class2(int x, int y){c = x; d = y;}
    void setE(){

        class1 arr[3];

        class1 arrTemp (0,0);
        arr[0] = arrTemp;

        class1 arrTemp1 (1,1);
        arr[1] = arrTemp1;

        class1 arrTemp2 (2,2);
        arr[2] = arrTemp2;


        cout << "Element 1, A = ";
        cout << arr[0].geta() << endl;

        cout << "Element 1, B = ";
        cout << arr[0].getb() << endl;

        cout << "Element 2, A = ";
        cout << arr[1].geta() << endl;

        cout << "Element 2, B = ";
        cout << arr[1].getb() << endl;     


        cout << "Element 3, A = ";
        cout << arr[2].geta() << endl;

        cout << "Element 3, B = ";
        cout << arr[2].getb() << endl;        

        class1 *pt;
        pt = &arr[0];

        e = pt;

        }
    class1* getpointer(){return e;}
    };

int main(){

        class2 testObj (1,2);

        testObj.setE();

        class1 *objPointer = testObj.getpointer(); 

        class1 tempclass1;

        tempclass1 = *(objPointer + 0);
        cout << "Element 1, A = ";
        cout << tempclass1.geta() << endl;

        cout << "Element 1, B = ";
        cout << tempclass1.getb() << endl;

        tempclass1 = *(objPointer + 1);
        cout << "Element 2, A = ";
        cout << tempclass1.geta() << endl;

        cout << "Element 2, B = ";
        cout << tempclass1.getb() << endl;  

        tempclass1 = *(objPointer + 1);
        cout << "Element 3, A = ";
        cout << tempclass1.geta() << endl;

        cout << "Element 3, B = ";
        cout << tempclass1.getb() << endl;             



    };
通过第一组
cout
语句,我验证了数据是否正确地进入了数组


看起来我得到了第一个元素的正确数据,但之后指针得到的数据与实际数组完全不同。我对C++很陌生,但是我认为>(指针+i) >等于<>代码> ARR[i] 。

在你的例子中至少有一个问题在这里:

void setE(){
    class1 arr[3];
    ...
    class1 *pt;
    pt = &arr[0];

    e = pt;
}
arr[3]
是一个本地对象,该对象在方法结束时被销毁。仍然可以使类成员
e
指向此对象


当方法结束时,
arr
内存被释放,可以由不同的对象自由覆盖。因此,当使用
e
读取内存时,您会观察到意外值。

class1 arr[3]-局部变量-在函数结束时消失。不要这样写代码-使用std::vector,而不是数组。这很有意义。你有没有关于如何避免这种情况的建议?据我所知,我无法拥有输出数组的方法。@Jarom您可以轻松地按值返回
std::array
std::vector
。解决方案取决于您实际需要实现的功能。我不会返回一个指向一系列值的指针,因为您丢失了有关数据大小的信息。我可能会使用@JesperJuhl建议,并切换
class1*e成为一个
std::数组e。然后将
getpointer()
修改为
const&std::array getData()const{return e;}
void setE(){
    class1 arr[3];
    ...
    class1 *pt;
    pt = &arr[0];

    e = pt;
}