Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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/5/ember.js/4.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++;循环遍历对象的地址_C++_This_Pointer Arithmetic_This Pointer - Fatal编程技术网

C++ C++;循环遍历对象的地址

C++ C++;循环遍历对象的地址,c++,this,pointer-arithmetic,this-pointer,C++,This,Pointer Arithmetic,This Pointer,对象(非动态)是内存中的数据块 是否有方法循环并打印对象中的每个项目 我试着用“这个”来做,但我总是出错 #include "stdafx.h" #include <iostream> #include "TestProject.h" using namespace std; class myclass { int someint = 10; double somedouble = 80000; int somearray[5] = {0, 1, 2,

对象(非动态)是内存中的数据块

是否有方法循环并打印对象中的每个项目

我试着用“这个”来做,但我总是出错

#include "stdafx.h"
#include <iostream>
#include "TestProject.h"

using namespace std;


class myclass {

    int someint = 10;
    double somedouble = 80000;
    int somearray[5] = {0, 1, 2, 3, 4};


public:   
    void somefunction();


};


void myclass::somefunction() {


    cout << "\n test \n" << this;

    myclass *somepointer;

    somepointer = this; 

    somepointer += 1;

    cout << "\n test2 \n" << *somepointer;
    //Error: no opperator '<<' matches these operands

}



int main() {


    myclass myobject;

    myobject.somefunction();

    return 0;
}
#包括“stdafx.h”
#包括
#包括“TestProject.h”
使用名称空间std;
类myclass{
int-someint=10;
double-somedouble=80000;
int somearray[5]={0,1,2,3,4};
公众:
void somefunction();
};
void myclass::somefunction(){

CUT< P>必须添加朋友Global <代码>:ST::OrthSuffer-P>C++,在设计上没有反射特性。这意味着在运行时没有类型的、独立于类型的元数据来访问类型元数据(例如,成员类及其类型)。所以,如果你想做的话(如果我正确理解的话),不能用C++来做。
此外,我也不确定你所说的“对象(非动态)”是什么意思。所有对象都是内存中的数据块,无论它们是否是动态分配的。

除非您的类完全由指针组成,否则按1在内存中进行迭代很可能会使您转到下一个对象。另外,您得到的原因是“错误:无运算器”实际上,指针比
char
大,因此它甚至不能只使用指针。您必须只使用
char
之类的指针。我也会看看这个,您可以保证“按声明的顺序增加地址”.但是有很多东西你不能保证你的类内存。这个想法是在一个循环中运行的。我同意,但我很确定动态块不能保证按顺序排列。至少,如果它们是用“new”初始化的,就不能。所以你把成员转换成变量来打印它们?没有其他方法来正确解释数据您也可以使用void或char指针,而不是使用字节中的实际成员移位来访问它们i指针指向someint(int*)pObjPrt,指针指向somedouble(double*)(pObjPrt+4),指针指向somearray(int*)(pObjPrt+12)有没有一种方法可以在不打包数据的情况下对填充进行解释?没有,没有。这是因为编译器将类成员的数据与成员的大小对齐,在我们的例子中,成员的大小是最大的(8B)。因此,指向somedouble的指针将从pObjPrt+8而不是pObjPrt+4开始,而somearray的地址将从pObjPrt+16而不是pObjPrt+12开始
#include "stdafx.h"
#include <iostream>

using namespace std;


class myclass {
    int someint;
    double somedouble;
    int somearray[5];
public: 
    myclass()
    {
        someint = 10;
        somedouble = 80000;
        somearray[0] = 0;
        somearray[1] = 1;
        somearray[2] = 2;
        somearray[3] = 3;
        somearray[4] = 4;
    }
    void somefunction();
    friend std::ostream& operator << (std::ostream& lhs, const myclass& rhs);
};


std::ostream& operator << (std::ostream& lhs, const myclass& rhs)
{
    lhs << "someint: " << rhs.someint << std::endl
        << "somedouble: " << rhs.somedouble << std::endl
        << "somearray: { ";

    for (int iIndex = 0; iIndex < 5; iIndex++)
    {
        if (iIndex == 4)
            lhs << rhs.somearray[iIndex] << " }" << std::endl;
        else
            lhs << rhs.somearray[iIndex] <<  ", ";
    }

    return lhs;
}


void myclass::somefunction() {


    cout << "\n test \n" << this;

    myclass *somepointer;

    somepointer = this; 

    somepointer += 1; // wrong pointer to object with `object + sizeof(object)` address, 
    // data probably has been corrupted

    cout << "\n test2 \n" << *somepointer; // displaying objects content
}

int main() {


    myclass myobject;

    myobject.somefunction();

    return 0;
}
#include "stdafx.h"
#include <iostream>

using namespace std;


#pragma pack (push, 1) // force data alignment to 1 byte

class myclass {
    int someint;
    double somedouble;
    int somearray[5];
public: 
    myclass()
    {
        someint = 10;
        somedouble = 80000;
        somearray[0] = 0;
        somearray[1] = 1;
        somearray[2] = 2;
        somearray[3] = 3;
        somearray[4] = 4;
    }
    void somefunction();
    friend std::ostream& operator << (std::ostream& lhs, const myclass& rhs);
};

#pragma pack (pop) // restore data alignment


std::ostream& operator << (std::ostream& lhs, const myclass& rhs)
{
    lhs << "someint: " << rhs.someint << std::endl
        << "somedouble: " << rhs.somedouble << std::endl
        << "somearray: { ";

    for (int iIndex = 0; iIndex < 5; iIndex++)
    {
        if (iIndex == 4)
            lhs << rhs.somearray[iIndex] << " }" << std::endl;
        else
            lhs << rhs.somearray[iIndex] <<  ", ";
    }

    return lhs;
}


void myclass::somefunction() {

    int* pSomeInt = (int*)this; // get someint address
    double *pSomeDouble = (double*)(pSomeInt + 1); // get somedouble address
    int* pSomeArray = (int*)(pSomeDouble + 1); // get somearray address

    std::cout << "someint: " << *pSomeInt << std::endl
        << "somedouble: " << *pSomeDouble << std::endl
        << "somearray: { ";

    for (int iIndex = 0; iIndex < 5; iIndex++)
    {
        if (iIndex == 4)
            std::cout << pSomeArray[iIndex] << " }" << std::endl;
        else
            std::cout << pSomeArray[iIndex] <<  ", ";
    }
}

int main() {


    myclass myobject;

    myobject.somefunction();

    return 0;
}