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

标签 c++ this pointer-arithmetic this-pointer

对象(不是动态的)是内存中的数据 block 。

有没有办法循环打印对象中的每一项?

我试着用“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++循环遍历对象的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33268042/

相关文章:

c++ - Windows std::ifstream::open() 问题

c++ - 具有函数指针和可变参数模板的 CUDA 内核

javascript - TypeScript 和 this 关键字(SharePoint 框架和 Vue)

c - 存储变量并取消引用它们

c++ - #define TheVLM(x) VLM::Global()->x TheVLM(Run());

javascript 对象、回调和 this 关键字

javascript - JS 调用() : Unexpected "this" context within function being called

C 多维数组不接受对其成员的修改

c - 我应该如何用指针算术来解决这个密码?

c++ - 如何在一个构造函数中使用两个可变模板参数来绑定(bind)两个函数?