C ++遍历对象的地址

时间:2015-10-21 19:52:30

标签: 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, 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;
}

我猜错了是因为类型不匹配。但我无法真正找到解决方案。是否有动态类型,或者我必须以某种方式测试类型?

2 个答案:

答案 0 :(得分:1)

您必须添加朋友全局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;
}

答案 1 :(得分:1)

C ++,按设计,没有反射功能。这意味着在运行时没有通用的,类型无关的方式来访问类型元数据(例如,如果是类及其类型的成员列表)。所以你想要做的事情(如果我理解正确的话)不能用C ++完成。

此外,我不确定对象(不是动态的)&#34;的含义。所有对象都是内存中的数据块,无论它们是否是动态分配的。