Вопрос

Hi today I am trying to clean up my code and I'm a little new on pointers and memcpy. What I am trying to do is something where I can do this.

pos()->x = 500; or even printf(pos->x)

CODE SO FAR - READ ONLY

struct vec3
{
    float x, y, z;
};

vec3 client::pos()
{
    vec3 vPos;
    memcpy(&vPos, &(*(float *)(state() + 0x1C)), sizeof(vec3));
    return vPos;
}

The above code will correctly return Z,Y and Z cords but obviously its not a pointer so cannot set one.

I really have not a clue how I could reverse the use off my above method well not reverse but rather use it both ways then defining two methods for this.

Below is an example of how I am currently setting pos

void client::setPos(float x, float y, float z)
{
    vec3 vPos = {
        x==NULL?pos().x:x,
        y==NULL?pos().y:y,
        z==NULL?pos().z:z
    };
    memcpy(&(*(float *)(state() + cordX)), &vPos, sizeof(vec3));
}

How can I "bind" those two methods into one by making my first one a pointer?

Это было полезно?

Решение

How about this:

vec3* client::pos()
{
    return (vec3*)(state() + 0x1C);
}

This way, pos() should return a vec3 pointer pointing to the internally stored data. Callers of pos() can then directly modify that data (if that's really what you want).

Note that you're giving out pointers to internal memory, you have to somehow deal with memory lifetime issues (eg. what happens if state() points to different memory later while somebody still uses the return value of pos()?).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top