循环遍历对象的地址



对象是内存中的数据块。

是否有一种方法可以循环遍历和打印对象中的每个项目?

我试着用'this'来做,但我总是出错。

#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;
}

我猜错误是因为类型不匹配。但我真的想不出解决办法。是否存在动态类型,还是我必须以某种方式测试类型?

必须添加好友全局std::ostream operator <<来显示对象的内容

#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;
}

c++在设计上没有反射特性。这意味着在运行时没有通用的、与类型无关的方式来访问类型元数据(例如,类及其类型的成员列表)。所以你想做的(如果我理解正确的话)在c++中是做不到的。

我也不确定你所说的"对象(不是动态的)"是什么意思。所有对象都是内存中的数据块,无论它们是否动态分配。

相关内容

  • 没有找到相关文章

最新更新