0

I am trying to make a pointer access class of which the goal is to simplifly access to known memory locations.

as for now I have this class:

template<class T = DWORD> struct Pointer
{
private://minimum 2 params
    std::vector<T> params;
    Pointer()
    {}
    Pointer(T a)
    {params.push_back(a);}
public:
    Pointer(T a, T b)
    {params.push_back(a);Pointer::Pointer(b);}
    Pointer(T a, T b, T c)
    {params.push_back(a);Pointer::Pointer(b,c);}
    Pointer(T a, T b, T c, T d)
    {params.push_back(a);Pointer::Pointer(b,c,d);}
    Pointer(T a, T b, T c, T d, T e)
    {params.push_back(a);Pointer::Pointer(b,c,d,e);}
    Pointer(T a, T b, T c, T d, T e, T f)
    {params.push_back(a);Pointer::Pointer(b,c,d,e,f);}
    Pointer(T a, T b, T c, T d, T e, T f, T g)
    {params.push_back(a);Pointer::Pointer(b,c,d,e,f,g);}
    //all the way to ... z
    T* ResolvePointer(/*,bool fallback = false*/) 
    {  
        T variable = params[0];
        try
        {
            auto it = params.begin();
            ++it;  

                for(; it != params.end(); ++it)
                    variable = *reinterpret_cast<T*>(variable) + *it;
        }
        catch(...)
        {   
            /*if(fallback){
                static char fallback_location[2048];
                variable = reinterpret_cast<T>(&fallback_location[0]);
            }else{*/
            variable = NULL;
            //} 
        }
        return reinterpret_cast<T*>(variable);
    }
    T* operator()()
    {
        return ResolvePointer();
    }
};

but whenever I call it

(for example:

Player[slot].Money = Pointer<int>(0x00400000+0x008E98EC,0xD8+(0x4*slot),0xE4,0x00,0x4)();),

the params vector always has only a :(

what am I doing wrong? How do I solve this?

P.S: I would like to use variadic templates but I am using MICROSOFT VISUAL C++ 11

  • Actually the CTP compiler is quite stable, which supports variadic templates. – BlueWanderer Jun 02 '13 at 05:17
  • @BlueWanderer still does it lack initializer_list support so I don't really know how to extract `args...`... `std::vector x({args...});` gives , like expected, errors. –  Jun 02 '13 at 05:23
  • CTP didn't update the STL, but at least you can make your own class support initializer list. And you can add initializer list support to STL yourself if you REALLY REALLY REALLY want it :X – BlueWanderer Jun 02 '13 at 05:35
  • 1
    @BlueWanderer [The C++ Standard Library is not STL](http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about) – Captain Obvlious Jun 02 '13 at 05:44

2 Answers2

1

The Pointer::Pointer(...) line in your constructors is NOT a constructor delegation, it's the declaration of a temporary object. You're basically telling the compiler to throw away everything but parameter a. The syntax you want - which VC11 does not support - is:

Pointer(T a, T b, T c) : Pointer(a, b) {
  params.push_back(c);
}

Honestly, this class seems poorly conceived to begin with. I think we could help you more if you ask about what goal you are trying to achieve with this class than how to implement it.

Casey
  • 41,449
  • 7
  • 95
  • 125
  • well what I basicly want to do is provide a base address and offsets, add the current value(dereference current address) and first offset, then make it a pointer and repeat the process until no more parameters are left. –  Jun 02 '13 at 05:48
  • VC11 doesn't, but the CTP does FWIW. – chris Jun 02 '13 at 05:50
1

Calling a constructor from another constructor in fact reconstruct the whole object, so it's not "sane".

Starting from C++11, delegation between constructors for a same object can be done, but it must be at initialization list level

Pointer(T a, T b, T c) : Pointer(a, b) 
{
  params.push_back(c);
}

But instead to define a set of functions, varadic template may help:

template<class T>
class Pointer
{
    std::vector<T> params;

    template<class A, class... AA>
    void push(const A& a, const AA&... aa)
    { push(aa...); params.push_back(a); } //< invert these calls depending on the order you wish

    void push() //the final recourse
    {}

public:
    template<class... AA>
    Pointer(const AA&... aa)
    { push(aa...); }

};
Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63