由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一个C++中function pointer的问题。
相关主题
请教一个C++的设计问题有大侠讲讲RTTI和exception的问题么?
python question, easy onehow to decode these data from users' input at a web site
golang 怎么把"image"库都放标准库里了?这个算法问题怎么处理?求思路
问个文字decoding的题目用python urlopen 抓mitbbs页面的问题
Python Browsermob Proxy Library on mac issueC# binary file reading problem
Java 提高performance问题有没有玩raspberry pi的同好
Python unicode问题这是什么编码?
问java applet的问题有没有象libmpeg2这样轻量级的H.264 decoder?
相关话题的讨论汇总
话题: dec话题: generator话题: map话题: class话题: base
进入Programming版参与讨论
1 (共1页)
g***l
发帖数: 2753
1
一个模块,原先是在C中写的,现在要用C++重新写,因为要添加新的功能。
情况是如下:
程序运行中,能够拿到一段数据,存在连续的memory中,uint8_t *data={30,23,.....
...},data[1]用来当tag_id用,不同tag_id要调用不同对象的处理函数,data[2]是表
示数据的长度。
在原来的C中,我是建立了一个 function pointer look up table,用data[1]来搜索,
返回相应的function pointer : void(*pfunc)(uint8_t* in_data_p,uint8_t * out_
result_p).
在C++中,现在每个对象都是一个单独的class,从base class继承来的。
class base_class
{
public:
uint8_t tag_id;
std::vector result;
public:
virtual ProcessData(uint8_t * in_data_p);
};
然后每个对象的class定义如下:
class obj_10: public base_class //10 is the tag_id
{
private:
// other members
public:
virtual ProcessData(uint8_t * in_data_p); //to override the one in base_
clss.
};
我定义了一个template
template
void obj_process_data(uint8_t *in_data_p,T& obj)
{
obj.ProcessData(in_data_p);
}
这样当我在程序中拿到 in_data_p的时间,进而拿到tag_id,调用 obj_process_data<
obj_10>(in_data_p)就可以了。
我现在是用switch(tag_ID){ case: 10... case 20...}这样来决定T的类型的,一共
100多个case,以后还要增加新的case,维护起来很麻烦啊。
请问有没有别的办法如同C中通过一个函数来查询一下,就可以决定T的类型?
谢谢。
EM
发帖数: 715
2
是不是可以用factory的design来写个meta program解决这个问题?

..

【在 g***l 的大作中提到】
: 一个模块,原先是在C中写的,现在要用C++重新写,因为要添加新的功能。
: 情况是如下:
: 程序运行中,能够拿到一段数据,存在连续的memory中,uint8_t *data={30,23,.....
: ...},data[1]用来当tag_id用,不同tag_id要调用不同对象的处理函数,data[2]是表
: 示数据的长度。
: 在原来的C中,我是建立了一个 function pointer look up table,用data[1]来搜索,
: 返回相应的function pointer : void(*pfunc)(uint8_t* in_data_p,uint8_t * out_
: result_p).
: 在C++中,现在每个对象都是一个单独的class,从base class继承来的。
: class base_class

X****r
发帖数: 3557
3
首先你是不是什么地方弄错了,运行时才确定的类型使用模板并无帮助。
其次这种情况一般来说是搞一个注册表把tag_id和相应的类对应起来,
实现上可以用一个宏减少复制类似的代码。

..

【在 g***l 的大作中提到】
: 一个模块,原先是在C中写的,现在要用C++重新写,因为要添加新的功能。
: 情况是如下:
: 程序运行中,能够拿到一段数据,存在连续的memory中,uint8_t *data={30,23,.....
: ...},data[1]用来当tag_id用,不同tag_id要调用不同对象的处理函数,data[2]是表
: 示数据的长度。
: 在原来的C中,我是建立了一个 function pointer look up table,用data[1]来搜索,
: 返回相应的function pointer : void(*pfunc)(uint8_t* in_data_p,uint8_t * out_
: result_p).
: 在C++中,现在每个对象都是一个单独的class,从base class继承来的。
: class base_class

g***l
发帖数: 2753
4
对,就是想搞一个LUT把tag_id和相应的类对应起来,可以不知道怎么样搞啊。
就是tag_id是动态生成的,宏也没法用啊。
有没有什么例子可以参考?
非常感谢

【在 X****r 的大作中提到】
: 首先你是不是什么地方弄错了,运行时才确定的类型使用模板并无帮助。
: 其次这种情况一般来说是搞一个注册表把tag_id和相应的类对应起来,
: 实现上可以用一个宏减少复制类似的代码。
:
: ..

t****t
发帖数: 6806
5
如果我没看错的话, 你的问题不在于决定T的类型, 而是如何从tag_id产生一个T的对象
. 有了T的对象, 你就可以用virtual function直接调用ProcessData()了.
原先的关系(tag_id -> processing_function)是通过查表来定的, 说明没什么规律;
所以你现在仍然还得查表. 现在这个新的关系(tag_id -> object of T)可以通过如下
的generator function来定:
template
class_base* new_object()
{
return new T;
}
typedef class_base* (*ObjectGenerator)();
然后定义一个表格
vector generator_table;
/* or you can use map generator_map; */
generator_table = {new_object, new_object /* .... */ };

..

【在 g***l 的大作中提到】
: 一个模块,原先是在C中写的,现在要用C++重新写,因为要添加新的功能。
: 情况是如下:
: 程序运行中,能够拿到一段数据,存在连续的memory中,uint8_t *data={30,23,.....
: ...},data[1]用来当tag_id用,不同tag_id要调用不同对象的处理函数,data[2]是表
: 示数据的长度。
: 在原来的C中,我是建立了一个 function pointer look up table,用data[1]来搜索,
: 返回相应的function pointer : void(*pfunc)(uint8_t* in_data_p,uint8_t * out_
: result_p).
: 在C++中,现在每个对象都是一个单独的class,从base class继承来的。
: class base_class

g***l
发帖数: 2753
6
对,就是需要根据tag_id来查表生成一个相应的对象。
非常感谢。我来试一试。

【在 t****t 的大作中提到】
: 如果我没看错的话, 你的问题不在于决定T的类型, 而是如何从tag_id产生一个T的对象
: . 有了T的对象, 你就可以用virtual function直接调用ProcessData()了.
: 原先的关系(tag_id -> processing_function)是通过查表来定的, 说明没什么规律;
: 所以你现在仍然还得查表. 现在这个新的关系(tag_id -> object of T)可以通过如下
: 的generator function来定:
: template
: class_base* new_object()
: {
: return new T;
: }

g***l
发帖数: 2753
7
老大的这个方法是可行的。可是又出现新的问题了。
我定义了如下接口:
template
dec_base* new_object()
{
return new T;
}
typedef dec_base* (*ObjectGenerator)();
static std::map generator_map;
template
void register_dec_handle(int tag_id)
{
generator_map.insert(make_pair(tag_id,new_object));
}
如果把以上代码跟 main() 代码放在一个main.cpp里面,编译链接都没有问题。但是如
果把以上代码放在另外一个单独的sample.cpp里面,在另外一个main.cpp中的main()调用
register_dec_handle()的时间,gcc就会报告
g++ main.cpp sample.cpp -o sample.exe
main.cpp: 在函数‘int main()’中:
main.cpp:21:11: 错误:‘generator_map’在此作用域中尚未声明
make: *** [all] Error 1
这事怎么回事? generator_map是在sample.cpp中定义的static变量,通过register_
dec_handle()来操作的。
/////////////////////main.cpp
#include
#include
#include
#include "sample.hpp"
using namespace std;
int main()
{
char data[]="this a test string";
#if 1
register_dec_handle(1);
register_dec_handle(2);

std::map::const_iterator itr;
for(itr=generator_map.begin();itr!=generator_map.end();itr++)
{
cout<first< ObjectGenerator func=itr->second;
dec_base* obj=(*func)();
obj->ProcessData(data);
}
#endif
return 0;
}
/////////////////sample.cpp
#include
#include
#include
#include "sample.hpp"
using namespace std;
dec_base::
dec_base()
{}
dec_base::
~dec_base()
{}
void dec_base::
ProcessData(char * in_data_p)
{
cout<<"dec_base::ProcessData"< }
dec_1::
dec_1()
{}
dec_1::
~dec_1()
{}
void dec_1::
ProcessData(char* in_data_p)
{
cout<<"dec_1::ProcessData"< }
bool dec_1::
export_data()
{
cout<<"dec_1::export_data()"< }
dec_2::
dec_2()
{}
dec_2::
~dec_2()
{}
void dec_2::
ProcessData(char* in_data_p)
{
cout<<"dec_2::ProcessData"< }
bool dec_2::
export_data_html()
{
cout<<"dec_2::export_data_html()"< }
template
dec_base* new_object()
{
return new T;
}
typedef dec_base* (*ObjectGenerator)();
std::map generator_map;
template
void register_dec_handle(int tag_id)
{
generator_map.insert(make_pair(tag_id,new_object));
}
////////////////sample.hpp
#ifndef WORK_SAMPLE_H
#define WORK_SAMPLE_H
#include
#include
class dec_base
{
public:
int tag_id;
std::vector result;

public:
dec_base();
~dec_base();
virtual void ProcessData(char* in_data_p);
};
class dec_1:public dec_base
{
private:
int size;
public:
dec_1();
~dec_1();
virtual void ProcessData(char* in_data_p);
bool export_data();
};
class dec_2:public dec_base
{
private:
int range;
public:
dec_2();
~dec_2();
virtual void ProcessData(char* in_data_p);
bool export_data_html();
};
////////////interface
template
dec_base* new_object();
typedef dec_base* (*ObjectGenerator)();
template
void register_dec_handle(int tag_id);
#endif

【在 t****t 的大作中提到】
: 如果我没看错的话, 你的问题不在于决定T的类型, 而是如何从tag_id产生一个T的对象
: . 有了T的对象, 你就可以用virtual function直接调用ProcessData()了.
: 原先的关系(tag_id -> processing_function)是通过查表来定的, 说明没什么规律;
: 所以你现在仍然还得查表. 现在这个新的关系(tag_id -> object of T)可以通过如下
: 的generator function来定:
: template
: class_base* new_object()
: {
: return new T;
: }

p***o
发帖数: 1252
8
You can have a non-template register_dec_handle function
(taking two paramters tag_id and ObjectGenerator) that
access generator_map directly and the template register_dec_handle
function should call that non-template one. Otherwise,
when the compiler instantiates the function template,
they need to see everything in the current translation
unit, which is obviously not possible here since generator_map
is static.
In short, if you know what is singleton, generator_map
should be one.

调用
对象
律;
如下

【在 g***l 的大作中提到】
: 老大的这个方法是可行的。可是又出现新的问题了。
: 我定义了如下接口:
: template
: dec_base* new_object()
: {
: return new T;
: }
: typedef dec_base* (*ObjectGenerator)();
: static std::map generator_map;
: template

g***l
发帖数: 2753
9
generator_map不能放到sample.hpp里面去吧?

有.

【在 t****t 的大作中提到】
: 如果我没看错的话, 你的问题不在于决定T的类型, 而是如何从tag_id产生一个T的对象
: . 有了T的对象, 你就可以用virtual function直接调用ProcessData()了.
: 原先的关系(tag_id -> processing_function)是通过查表来定的, 说明没什么规律;
: 所以你现在仍然还得查表. 现在这个新的关系(tag_id -> object of T)可以通过如下
: 的generator function来定:
: template
: class_base* new_object()
: {
: return new T;
: }

t****t
发帖数: 6806
10
简单的回答: 你需要让register_dec_handle看到generator_map.
换句话说, 生成register_dec_handle的compilation unit需要能看到generator_map.
而register_dec_handle是一个模板, 你用的隐式生成(implicit instantiation)所以
调用者, 即main()需要能看到generator_map.
从另一方面来说, generator_map反正需要经常被查询, 所以应该能被所有查询者看到.
所以声明成static没什么意义. file-scope static的作用是防止名污染, 你这个不在
此列.
长的回答:
这个和我自己的一个程序很类似, 所以我多说两句.
如果是我做, 我会把实现和调用完全分开. 这样分割:
*** base.h: 基类, 只留基类的基本接口和小的inline实现; factory声明, 给定ID产
生类. 这个文件是实现和调用唯一公用的部分.
class base_class { ... };
base_class* decoder_factory(uint_8 ID);
*** base_private.h: include "base.h", 加所有子类的公共声明, 和表格的生成器.
比如
template class decoder;
typedef base_class* (*Generator)();
template base_class* generator() { return new decoder; }
extern map map_generator;
template inline void register_myself() {
map_generator.insert(make_pair(ID, generator));
}
*** base.cpp:基类的实现, 和map_generator的定义, factory的定义
*** dec_1.cpp: include "base_private.h", 加decoder<1>的定义. 比如
template <>
class decoder<1> : public base { .... };
static int dummy = register_myself<1>(), 0;
当然我这是举个例子, 具体的实现还要改, 比如用一个singleton来把以上的都包装成
一个大的factory(不一定是好事); 比如说子类根本不必要是模板; 等等.
这个设计的关键是不仅实现和使用是分开的, 每个实现也都完全相互独立(除了必须遵
守基类的规范以外). 你写一个实现就是一个实现, 不需要到任何公共的地方登记它.
不过, 以上的程序有个毛病, 所有的register_myself都要等map_generator初始化以后
才能用. 所以我自己是把map_generator包装在一个singleton里的, 调用到必然已经初
始化好了.

【在 g***l 的大作中提到】
: 老大的这个方法是可行的。可是又出现新的问题了。
: 我定义了如下接口:
: template
: dec_base* new_object()
: {
: return new T;
: }
: typedef dec_base* (*ObjectGenerator)();
: static std::map generator_map;
: template

相关主题
Java 提高performance问题有大侠讲讲RTTI和exception的问题么?
Python unicode问题how to decode these data from users' input at a web site
问java applet的问题这个算法问题怎么处理?求思路
进入Programming版参与讨论
t****t
发帖数: 6806
11
make it extern (declaration only). see my previous post.

【在 g***l 的大作中提到】
: generator_map不能放到sample.hpp里面去吧?
:
: 有.

g***l
发帖数: 2753
12
非常感谢!
"所有的register_myself都要等map_generator初始化以后才能用" ??
如果把map_generator设成global的话,这个在compile的时间就已经初始化了,为什么
吗还要等待? 还是要注意初始化的顺序问题?

到.

【在 t****t 的大作中提到】
: 简单的回答: 你需要让register_dec_handle看到generator_map.
: 换句话说, 生成register_dec_handle的compilation unit需要能看到generator_map.
: 而register_dec_handle是一个模板, 你用的隐式生成(implicit instantiation)所以
: 调用者, 即main()需要能看到generator_map.
: 从另一方面来说, generator_map反正需要经常被查询, 所以应该能被所有查询者看到.
: 所以声明成static没什么意义. file-scope static的作用是防止名污染, 你这个不在
: 此列.
: 长的回答:
: 这个和我自己的一个程序很类似, 所以我多说两句.
: 如果是我做, 我会把实现和调用完全分开. 这样分割:

g***l
发帖数: 2753
13
好像这样也不行。
我在sample.cpp里面定义了std::map generator_map;
然后在sample.hpp里面声明extern std::map generator_map;
最后编译的时间会报错: undefined reference :void register_dec_handle(int)

【在 t****t 的大作中提到】
: make it extern (declaration only). see my previous post.
t****t
发帖数: 6806
14
in c++, except POD types, there is no "compile-time" initialization. almost
all constructors are executed runtime. so the initialization order is
important.

【在 g***l 的大作中提到】
: 非常感谢!
: "所有的register_myself都要等map_generator初始化以后才能用" ??
: 如果把map_generator设成global的话,这个在compile的时间就已经初始化了,为什么
: 吗还要等待? 还是要注意初始化的顺序问题?
:
: 到.

t****t
发帖数: 6806
15
OK, you need some c++ 101. obviously undefined reference of "register_dec_
handle" has nothing to do with definition/declaration of generator_map.

map;

【在 g***l 的大作中提到】
: 好像这样也不行。
: 我在sample.cpp里面定义了std::map generator_map;
: 然后在sample.hpp里面声明extern std::map generator_map;
: 最后编译的时间会报错: undefined reference :void register_dec_handle(int)

g***l
发帖数: 2753
16
this is the complete error message. I think it is still related to function
template.
template
void register_dec_handle(int tag_id);
was declared in sample.hpp and was implemented in sample.cpp.
both class dec_1 and class dec_2 were declared in sample.hpp and implemented
in sample.cpp.
In main.cpp, #include "sample.hpp", refer to above code.
////////////////////////////////////
$g++ main.cpp sample.cpp sample.hpp -o sample
/tmp/ccPFwZuH.o: In function `main':
main.cpp:(.text+0x3f): undefined reference to `void register_dec_handle 1>(int)'
main.cpp:(.text+0x4b): undefined reference to `void register_dec_handle 2>(int)'
collect2: ld returned 1 exit status

【在 t****t 的大作中提到】
: OK, you need some c++ 101. obviously undefined reference of "register_dec_
: handle" has nothing to do with definition/declaration of generator_map.
:
: map;

g***l
发帖数: 2753
17
hehe. got it resolved.
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
it is the same issue as the generator due to template.
Anyway, thank you so much!

【在 t****t 的大作中提到】
: OK, you need some c++ 101. obviously undefined reference of "register_dec_
: handle" has nothing to do with definition/declaration of generator_map.
:
: map;

b***i
发帖数: 3043
18
我提醒你一下,template register_dec+handle(int tag_id)具体编译的时
候对某个类是否展开并放进obj,取决于那个类型是否在该cpp中被用到。编译时候没看
见,比如dec_1没出现,就不在obj,自然链接看不见。
所以,你前面还是没有说清楚。你提到的要用简单的表达式生成某个类型的一个实例,
是纯粹运行时才知道的,在编译时根本不知道。这就好像你在写一个编译器,在后期扩
展新类型的时候,不需要更改已有的架构。
所以你可以回到第一个方案,就是利用虚函数来实现。
那么,每个新类型register的时候把一个整数的值和自己类里面的函数的实现登记。这
个函数要extern "C",这个函数在这个类cpp里面生成这个类的一个实例。这样,一个
函数createAnInstance(int tag_id)里面{ base * A=getyourfunction, A->
createInstance();}就实现了生成这个类的实例这个过程。而登记类生成函数分布在
每个类自己的文件中。
另外,有人提到宏,借助一个宏,如果把tag_id变string,自己好好写个宏,可以写
create(int, x) 等价于 int x = createAnInstance("int");
具体还有很多要说的,但是你要的东西是可做的,也不难。但要搞清楚编译和链接的过
程。

function
implemented

【在 g***l 的大作中提到】
: this is the complete error message. I think it is still related to function
: template.
: template
: void register_dec_handle(int tag_id);
: was declared in sample.hpp and was implemented in sample.cpp.
: both class dec_1 and class dec_2 were declared in sample.hpp and implemented
: in sample.cpp.
: In main.cpp, #include "sample.hpp", refer to above code.
: ////////////////////////////////////
: $g++ main.cpp sample.cpp sample.hpp -o sample

g***l
发帖数: 2753
19
谢谢。还要谢谢thrust.
template的东西我以前基本没有怎么用过,很多东西不知道。这两天为了解决这个问题
,基本都在看资料学习,知道了不少东西。
我原先认为compiler在翻译template的时间,只是做个标记,等到最后链接的时间再去
obj里面找。
现在的选择要么是写一个专门的singleton,这个是来代替前面的global generator. 要
么就是回到你说的那种,类似于C里面的function Pointer LUT。

【在 b***i 的大作中提到】
: 我提醒你一下,template register_dec+handle(int tag_id)具体编译的时
: 候对某个类是否展开并放进obj,取决于那个类型是否在该cpp中被用到。编译时候没看
: 见,比如dec_1没出现,就不在obj,自然链接看不见。
: 所以,你前面还是没有说清楚。你提到的要用简单的表达式生成某个类型的一个实例,
: 是纯粹运行时才知道的,在编译时根本不知道。这就好像你在写一个编译器,在后期扩
: 展新类型的时候,不需要更改已有的架构。
: 所以你可以回到第一个方案,就是利用虚函数来实现。
: 那么,每个新类型register的时候把一个整数的值和自己类里面的函数的实现登记。这
: 个函数要extern "C",这个函数在这个类cpp里面生成这个类的一个实例。这样,一个
: 函数createAnInstance(int tag_id)里面{ base * A=getyourfunction, A->

t****t
发帖数: 6806
20
我那个方案的接口仍然是虚函数, 但是factory用的是模板. 按照我的方案, 在每个类
文件里会使用一次register_myself用来调用map.insert(..., generator),
而generator又会调用new decoder从而instantiate整个类. 这个过程对使用
者完全是不可见的, 使用者能看到的是两个东西: 一个是虚函数的接口, 定义在base_
class里, 另一个是decoder_factory, 它查找map调用相应的generator(已生成)返
回正确的对象.
这一套pattern我自己在用, 所以我很清楚它一定会work:)

【在 b***i 的大作中提到】
: 我提醒你一下,template register_dec+handle(int tag_id)具体编译的时
: 候对某个类是否展开并放进obj,取决于那个类型是否在该cpp中被用到。编译时候没看
: 见,比如dec_1没出现,就不在obj,自然链接看不见。
: 所以,你前面还是没有说清楚。你提到的要用简单的表达式生成某个类型的一个实例,
: 是纯粹运行时才知道的,在编译时根本不知道。这就好像你在写一个编译器,在后期扩
: 展新类型的时候,不需要更改已有的架构。
: 所以你可以回到第一个方案,就是利用虚函数来实现。
: 那么,每个新类型register的时候把一个整数的值和自己类里面的函数的实现登记。这
: 个函数要extern "C",这个函数在这个类cpp里面生成这个类的一个实例。这样,一个
: 函数createAnInstance(int tag_id)里面{ base * A=getyourfunction, A->

相关主题
用python urlopen 抓mitbbs页面的问题这是什么编码?
C# binary file reading problem有没有象libmpeg2这样轻量级的H.264 decoder?
有没有玩raspberry pi的同好求教, python 对于很奇怪的字符的encoding 怎么处理?
进入Programming版参与讨论
b***i
发帖数: 3043
21
刚要睡觉,突然想起来,.hpp要声明templateregister_dec,在每个类的cpp中
要实现这个类型的register_dec,比如dec_1, dec_2,你是不是忘了实现了?我知道你
在sample.cpp里面定义 了两个类,这两个类的register_dec要定义(实现)。你先看
看这样做能否通过。
这样每个类自己编译的时候要看到那个.h,就展开那个类的template,就可以把这个类
型的实现的编译结果放在自己的obj里面。

【在 g***l 的大作中提到】
: 谢谢。还要谢谢thrust.
: template的东西我以前基本没有怎么用过,很多东西不知道。这两天为了解决这个问题
: ,基本都在看资料学习,知道了不少东西。
: 我原先认为compiler在翻译template的时间,只是做个标记,等到最后链接的时间再去
: obj里面找。
: 现在的选择要么是写一个专门的singleton,这个是来代替前面的global generator. 要
: 么就是回到你说的那种,类似于C里面的function Pointer LUT。

t****t
发帖数: 6806
22
that's exactly my pattern.

【在 b***i 的大作中提到】
: 刚要睡觉,突然想起来,.hpp要声明templateregister_dec,在每个类的cpp中
: 要实现这个类型的register_dec,比如dec_1, dec_2,你是不是忘了实现了?我知道你
: 在sample.cpp里面定义 了两个类,这两个类的register_dec要定义(实现)。你先看
: 看这样做能否通过。
: 这样每个类自己编译的时候要看到那个.h,就展开那个类的template,就可以把这个类
: 型的实现的编译结果放在自己的obj里面。

t****t
发帖数: 6806
23

you may choose to do so, if you know exactly what parameter you use,
which is true in your case. in this case, you can hide implementation and
only provide declaration. in fact in your case, you don't even have to
provide
declaration, you can only provide the declaration of base class.
client don't have to know what subclass you implemented.

【在 g***l 的大作中提到】
: 谢谢。还要谢谢thrust.
: template的东西我以前基本没有怎么用过,很多东西不知道。这两天为了解决这个问题
: ,基本都在看资料学习,知道了不少东西。
: 我原先认为compiler在翻译template的时间,只是做个标记,等到最后链接的时间再去
: obj里面找。
: 现在的选择要么是写一个专门的singleton,这个是来代替前面的global generator. 要
: 么就是回到你说的那种,类似于C里面的function Pointer LUT。

g***l
发帖数: 2753
24
对,我在sample.cpp里面加上了这两个函数的声明就可以编译通过了。
template void register_dec_handle(int);

template void register_dec_handle(int)

【在 b***i 的大作中提到】
: 刚要睡觉,突然想起来,.hpp要声明templateregister_dec,在每个类的cpp中
: 要实现这个类型的register_dec,比如dec_1, dec_2,你是不是忘了实现了?我知道你
: 在sample.cpp里面定义 了两个类,这两个类的register_dec要定义(实现)。你先看
: 看这样做能否通过。
: 这样每个类自己编译的时候要看到那个.h,就展开那个类的template,就可以把这个类
: 型的实现的编译结果放在自己的obj里面。

g***l
发帖数: 2753
25
yes, the client only know the base class.
even in old C code, the client know nothing about the details of the decoder
at all.

【在 t****t 的大作中提到】
:
: you may choose to do so, if you know exactly what parameter you use,
: which is true in your case. in this case, you can hide implementation and
: only provide declaration. in fact in your case, you don't even have to
: provide
: declaration, you can only provide the declaration of base class.
: client don't have to know what subclass you implemented.

b***i
发帖数: 3043
26
glad to know!
are you writing compiler? Like a matlab?

【在 g***l 的大作中提到】
: 对,我在sample.cpp里面加上了这两个函数的声明就可以编译通过了。
: template void register_dec_handle(int);
: 和
: template void register_dec_handle(int)

t****t
发帖数: 6806
27
obviously it's a serdes...

【在 b***i 的大作中提到】
: glad to know!
: are you writing compiler? Like a matlab?

1 (共1页)
进入Programming版参与讨论
相关主题
有没有象libmpeg2这样轻量级的H.264 decoder?Python Browsermob Proxy Library on mac issue
求教, python 对于很奇怪的字符的encoding 怎么处理?Java 提高performance问题
Postman传http post 总将 冒号 (:) 转成 %3A 怎么办?Python unicode问题
有一个文件夹里有大概1000个文件。我有以下的Python语句调用后(转载)问java applet的问题
请教一个C++的设计问题有大侠讲讲RTTI和exception的问题么?
python question, easy onehow to decode these data from users' input at a web site
golang 怎么把"image"库都放标准库里了?这个算法问题怎么处理?求思路
问个文字decoding的题目用python urlopen 抓mitbbs页面的问题
相关话题的讨论汇总
话题: dec话题: generator话题: map话题: class话题: base