c++ - 从 this->class 构造函数调用类构造函数(不同的参数)?

标签 c++ visual-c++ visual-studio-2012 c++11 variadic-functions

我正在尝试制作一个指针访问类,其目标是简单地访问已知的内存位置。

至于现在我有这个类:

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();
    }
};

但每当我调用它时

(例如:

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

params vector 始终 只有a :(

我做错了什么?我该如何解决这个问题?

P.S:我想使用可变参数模板,但我正在使用 MICROSOFT VISUAL C++ 11

最佳答案

从另一个构造函数调用一个构造函数实际上是重建了整个对象,所以它并不“正常”。

从C++11开始,同一个对象的构造函数之间可以进行委托(delegate),但必须在初始化列表级别

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

但是为了定义一组函数,varadic 模板可能会有所帮助:

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...); }

};

关于c++ - 从 this->class 构造函数调用类构造函数(不同的参数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879990/

相关文章:

visual-studio - 如何防止 Visual Studio 2012 在文件中查找默认为 "Current Document"?

c++ - 请求 “…” 中的成员 “…” 是非类类型 “…”

c++ - 从 vector 列表中删除 vector 元素 C++

c++ - VC++ 允许为 STL 容器使用 const 类型。为什么?

visual-c++ - 链接到所有 Visual Studio $ 变量

c++ - 链接器诊断 : "exe not found or not built by the last incremental link; performing full link", 为什么?

c++ - 将 python 与 c++ 接口(interface)

c++ - MinGW g++ 4.8.1-4 无法识别 -std=c++14

c++ - Visual Studio 找不到任何标准 C++ 库文件

azure - Development Fabric中的Azure服务SSL错误