存储4个不同的名称和指针以供将来使用

时间:2013-03-01 09:20:46

标签: c++ visual-c++ function-pointers

我有一个名为Pagemanager的函数类,就像这样

class Page : public Ipage //Ipage is abstract with pure virtual function
{
public: 
      page(void);
      ~page(void);
      virtual int FillDbgPage(char* sz, unsigned int max_bytes, 
      char* arg1, char*    arg2,  char* arg3, char* arg4){return 0;}
 };

//Pagemanager.h
class pageManager : public IPage
{
public:
      pageManager(void);
      ~pageManager(void);

      //client calls this function to register himself as a page content owners
      void registerPage(char* PageName, Page* PagePtr)

      //webserver calls this function to request page update
      int FillDbgPage(char* sz, unsigned int max_bytes, 
      char* arg1, char* arg2,  char* arg3, char* arg4);

Private:
       char name[10];    //stores the name of the page 
       Ipage* storePagePtr; //stores the address of the page
};

//pageManager.cpp
int pageManager::FillDbgPage(char* sz, unsigned int max_bytes, char* arg1, 
char*  arg2,  char* arg3, char* arg4)
{
if(arg4!=NULL)
{
  if (strcmp(arg4, name)==0)
  {
   cout<<"Page"<<arg4<<"Found"<<endl;

     if(storePagePtr==Null)
      cout<<"storePagePtr not initialized"<<endl;
     else
       int len = storePagePtr->FillDbgPage(sz, max_bytes, arg1, arg2, arg3, arg4);
   else
     cout<<"Sorry"<<arg4<<not found!<<endl;
else
{
 strcat(sz, ("<a href = http://127.0.0.1/dbg_image_pipe?arg4="));
 strcat(sz, name);
 strcat(sz,">");
 strcat(sz, name);
 strcat)sz, "<br>");
 strcat(sz, ("</a>\n"));
}
return strlen(sz);
}
void pageManager::RegisterPage(char* PageName, Page *PagePtr)
{
 storePagePtr= PagePtr;
 strcpy(name, PageName);
}


//this is code from cpp file which will be done by end users for example
pageManager systemRegister;
class myPage1 : public Page
{
public:
       virtual int FillDbgPage(char* sz, unsigned int max_bytes, char* arg1, 
       char* arg2,  char* arg3, char* arg4)
{
      //sample content that user will print in the page he created
      sprintf(sz, "Hello World"! <br> This is my page!);
      return strlen(sz);
}

myPage1 Page1;

void module1_initialize()
{
systemRegister.RegisterPage("Page1", &Page1);
}

问题是现在1页被创建并存储在名称和sotrePagePtr中 但如果有4页,我该怎么办让我们说用户想要4个不同的文件,如何正确保存它们以便将来显示它们变得更容易

1 个答案:

答案 0 :(得分:0)

使用例如std::vector<Page*>RegisterPage使用push_back方法。 如果要为每个页面添加名称,则应在结构或类中处理页面及其名称,并将std::vector与此结构或类一起使用