你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C语言的转换表 C++怎么实现?

[复制链接]
byronsong 提问时间:2019-6-28 14:57 /
1.成员函数指针
转自  http://www.cppblog.com/dragon/archive/2010/12/02/135250.html
/*
*测试成员函数指针数组的小程序
*/

#include
<iostream>
using namespace std;

class Test
{
public:
    Test();
    ~Test();

private:
    void add5(){ res+=5;}
    void add6(){ res+=6;}

    void (Test::*add[2])();//这个2至关重要,在VC下没写会报错,但在QT里没报,但析构时出错!
public:
    void DoAddAction();
    void Display();
private:
    int res;
}
;


Test::Test()
{
    add[0]=&Test::add5;//注意这里的写法
    add[1]=&Test::add6;
    res=0;
}


Test::
~Test()
{

}


void Test:oAddAction()
{
    for (int i=0;i<2;i++)
    {
        (this->*add)();//使用类成员函数指针必须有“->*”或“.*”的调用
    }
}


void Test:isplay()
{
    cout<<"The res is:"<<res<<endl;
}


int main()
{
    Test * test=new Test();
    test->DoAddAction();
    test->Display();
    delete test;
    return 0;
}



2.c++中成员函数指针数组定义和初始化方法

转自  http://www.cnblogs.com/sunowfox/p/6875051.html
实际项目中经常遇到很多类似操作,比如命令码对应执行函数等,对于此类操作,比较好的方式是使用const数组,将命令码和操作函数绑定在一起,通过查表方式找到操作函数,并执行操作函数。这样可以简化代码,降低复杂度,在c中这种方式很好实现,在c++中会稍微麻烦一些。
以串口命令解析执行为例,首先定义一个结构体,定义操作函数的指针类型:
struct T_ShellInfo{    string cmd;    void (* DealFunc)(const vector<string> &vectStr);    string desc;};
定义命令解析执行类,处理函数要定义成static,定义一个const static的数组:
[url=][/url]
class CShell{public:    CShell();    ~CShell();    void RecvCmd();private:       enum{SHELL_INFO_NUM_MAX=10};    void GetCmd(const char *cmdStr, vector<string> &vectStr);    void Deal(const vector<string> &vectStr);        static void CmdHelp(const vector<string> &vectStr);    static void CmdLed(const vector<string> &vectStr);    static void CmdTask(const vector<string> &vectStr);    static void CmdDisk(const vector<string> &vectStr);    static void CmdTime(const vector<string> &vectStr);    static void CmdReboot(const vector<string> &vectStr);       CShell *_self;    const static T_ShellInfo _shellInfo[SHELL_INFO_NUM_MAX]; };[url=][/url]

实现时,先初始化数组
[url=][/url]
const T_ShellInfo CShell::_shellInfo[SHELL_INFO_NUM_MAX]={    {"?",       CmdHelp,    "show all cmd"},    {"help",    CmdHelp,    "show all cmd"},    {"time",    CmdTime,    "show sys time"},    {"task",    CmdTask,    "show all task info"},    {"disk",    CmdDisk,    "disk cmd [info ls cat rm format]"},    {"led",     CmdLed,     "set led [normal charge alarm]"},    {"reboot",  CmdReboot,  "reboot sys"},        {"",NULL,""}};[url=][/url]

串口收到命令后,只要遍历此数组,找到相同命令码的执行函数并执行即可
[url=][/url]
void CShell:eal(const vector<string> &vectStr){    for (u8 i = 0; i< SHELL_INFO_NUM_MAX; i++)    {        if (_shellInfo.cmd.length() == 0)        {            break;        }        if (_shellInfo.cmd == vectStr[0])        {            _shellInfo.DealFunc(vectStr);            return;        }    }    printf("cmd \"%s\" err!\r\n", vectStr[0].c_str());}[url=][/url]

然后逐个实现对应操作函数:
[url=][/url]
void CShell::CmdTask(const vector<string> &vectStr){    u8 pcWriteBuffer[500]={0};    //u8 *pcWriteBuffer = new u8[500]; //这个值要小心,太小会导致数组溢出,太大会导致堆栈溢出    printf("=================================================\r\n");    printf("任务名      任务状态 优先级   剩余栈 任务序号\r\n");    vTaskList((char *)&pcWriteBuffer);    printf("%s\r\n", pcWriteBuffer);    //delete[] pcWriteBuffer;}[url=][/url]



3.采用第一节 所述方法keil5编译成功,第二节所述方法未成功。

但成员函数的指针所占内存空间并非,通常的4字节,内存浪费严重。
没想到解决途径,留个疑问。





收藏 评论1 发布时间:2019-6-28 14:57

举报

1个回答
edmundlee 回答时间:2019-6-28 15:55:55
将T_ShellInfo的 两个String 都改为char的指针(char*)然后把String放于const char[][], 就不会浪费了

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版