void指针参数,传递缓冲区

时间:2013-10-28 10:07:27

标签: c++

我有一个对象缓冲区实现,如下所示:

class obj_buffer_t
{
public:
    obj_buffer_t(char *heap, size_t size, size_t cnt);
            // Some other stuff
private:
            // Some other stuff
};

record_objbuf创建为静态缓冲区:

static obj_buffer_t::buffer_t<obj_buffer_min_heap_size_t< sizeof(Record_XY), RECORD_SIZE>::result> record_buffer;
obj_buffer_t record_objbuf(record_buffer.heap, sizeof record_buffer.heap, RECORD_SIZE);

在我的程序中,我想读取记录并在此缓冲区中创建它们,但我如何调用我的方法?

方法签名如下:

Record * getRecord(unsigned & addr, Info & info, void * objbuf = NULL);

我的电话:

record = tm.getRecord(p, info, static_cast<obj_buffer_t *>(record_objbuf));

编译器给出了错误:从obj_buffer_t类型到obj_buffer_t*

类型的static_cast无效

如果我根本不使用演员表,我会:

没有用于调用getRecord(uint32_t&, Info&, obj_buffer_t&)

的匹配函数

我在这里做错了什么?

THX!

1 个答案:

答案 0 :(得分:2)

错误'从类型obj_buffer_t到类型obj_buffer_t *的无效static_cast'表示record_objbuff是对象本身,而不是指向该对象的指针。你的功能需要指针。因此,您应该使用一元&运算符来获取对象的地址作为指针:

record = tm.getRecord( p, info, (void*)&record_objbuf );