小编Kun*_*shu的帖子

在为类方法执行指针部分特化时,获取"非法使用显式模板参数"

你好,我有部分专业化的问题.我想要做的是拥有一个具有模板成员函数的类,该函数将给定值解释为用户指定的值.例如,类名是Value,这是我想要做的片段:

int *ptr1 = new int;
*ptr1 = 10;
Value val1 = ptr1;
int *ptr2 = val1.getValue<int*>();

Value val2 = 1;
int testVal = val2.getValue<int>();
Run Code Online (Sandbox Code Playgroud)

以下是我实现此类的方法:

struct Value {

    Value(void *p) : val1(p){}
    Value(int i) : val2(i){}

    template<typename T>
    T getValue();

    void *val1;
    int val2;
};

template<typename T>
T*  Value::getValue<T*>() {
    return reinterpret_cast<T*>(val1);
}

template<>
int Value::getValue<int>() {
    return val2;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我收到以下错误:

错误C2768:'Value :: getValue':非法使用显式模板参数

基本上它抱怨代码的指针模板部分:

template<typename T>
T* Value::getValue<T*>() {
    return reinterpret_cast<T*>(val1);
}
Run Code Online (Sandbox Code Playgroud)

我知道这个问题可以通过一个简单的联合来实现,但是这个代码是一个更大代码的精简版本.

有人知道问题可能是什么吗?我想要做的是在使用指针时分离一个代码,而在不使用指针时分开.我真的被卡住了,我总是调查而不是问,但我没有找到任何关于它的好信息.

c++ syntax templates template-specialization

13
推荐指数
1
解决办法
1万
查看次数

指向不同地址的vTables和函数指针

我最近在bitsquid博客上阅读了一篇关于如何管理内存的文章,作者开始讨论vtable以及编译器如何将类添加到类中.这是该文章的链接.因为我几乎不知道关于这个问题的事情,所以我开始在网上寻找解释.我来到了这个链接.根据我读到的内容,我制作了以下代码:

char cache[24];

printf("Size of int = %d\n", sizeof(int));
printf("Size of A = %d\n", sizeof(A));

A* a = new(cache)A(0,0);
printf("%s\n",cache);
printf("vTable    : %d\n",*((int*)cache));
printf("cache addr: %d\n",&cache);

int* funcPointer = (int*)(*((int*)cache));
printf("A::sayHello: %d\n",&A::sayHello);
printf("funcPointer: %d\n",*funcPointer);
Run Code Online (Sandbox Code Playgroud)

A是一个具有两个整数成员和一个虚函数的类sayHello().

编辑:这是类定义:

class A {
public:
    int _x;
    int _y;
public:
    A(int x, int y) : _x(x), _y(y){ }
    virtual void sayHello() { printf("Hello You!"); }
};
Run Code Online (Sandbox Code Playgroud)

基本上我试图做的是看看vtable中的指针是否指向与我所从的地址相同的位置&A::sayHello,但问题是当我运行程序时,vtable中指针内的地址和sayHello()地址总是有区别的295.有谁知道为什么会发生这种情况?是否有某种标题被添加,我错过了?我在64位机器上运行visual studio express 2008.

从我调试的地址返回的 …

c++ memory-management vtable

4
推荐指数
1
解决办法
1239
查看次数

使用预先分配的内存和数组管理析构函数

您好我正在尝试使用预分配的内存创建对象和数组.例如,我有以下代码:

int * prealloc = (int*)malloc(sizeof(Test));

Test *arr = new(prealloc) Test();
Run Code Online (Sandbox Code Playgroud)

测试定义如下:

class Test {
public:
    Test() {
        printf("In Constructor\n");
    }
    ~Test() {
        printf("In Destructor\n");
    }

    int val;
};
Run Code Online (Sandbox Code Playgroud)

在这种情况下如果我调用delete它实际上会释放内存不好,b/c也许我正在使用某种类型的内存管理器,所以这肯定会引起一些问题.我在互联网上搜索,我找到的唯一解决方案是明确调用析构函数然后调用free:

arr->~Test();
free(arr);
Run Code Online (Sandbox Code Playgroud)

还有另一种方法吗?有没有办法调用删除并告诉它只是调用析构函数而不是释放内存?

我的第二个问题是在使用数组时,就像前面的例子一样,你可以传递给新的预分配内存:

int * prealloc2 = (int*)malloc(sizeof(Test) * 10);
Test *arr2 = new(prealloc2) Test[10];
Run Code Online (Sandbox Code Playgroud)

如果我调用delete[]它不仅会为数组中的每个元素调用析构函数,而且还会释放我不想要的内存.我发现它应该完成的唯一方法是遍历数组并显式调用析构函数,然后调用free.与常规无数组运算符一样,有一种方法可以告诉操作员只调用析构函数而不释放内存吗?

我注意到的一件事是数组的new运算符实际上将使用前4个字节来存储数组的大小(我只在Visual Studio中使用32位构建测试了这个)这将帮助我知道有多少元素数组有,但仍有一个问题.如果数组是指针数组怎么办?例如:

Test **arr2 = new Test*[10];
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这些问题.

c++ memory-management new-operator

1
推荐指数
1
解决办法
1521
查看次数