Search code examples
c++thispointer-arithmeticthis-pointer

C++ Cycle through the addresses of an object


Objects (that are not dynamic) are blocks of data in memory.

Is there a way to cycle through and print each item in an object?

I tried doing it with 'this' but I keep getting errors.

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

I'm guessing the error is because the types don't match. But I can't really figure a solution. Is there a dynamic type, or do I have to test the type somehow?


Solution

  • You must add friend global std::ostream operator << to display content of object

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

    as you want to get to the object member using its pointers shifts I post another program

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