2017-05-04 60 views
0

这些是头文件我的类函数:类函数的指针

public: 
    hunter(string aSpecies);   // create a hunter of the given species 
    void recordKills(string kill); // add a new kill to the end of the hunter's list of kills 
    string *theKills();    // return a pointer to the array of all kills by this hunter 
    int numberOfKills();    // how many kills have been recorded 

和类变量:

private: 
    string kill; 
    int numberkilled; 
    string kills[20]; 

我不知道该如何处理“串* theKills()”

我试图做这样:

string hunter::*theKills(){ 
    pointer = kills; 
    return pointer; 
} 

与*不能识别kills作为我的类变量的一部分,但我们应该使用相同的函数名称。

+2

不应该是'string * hunter :: theKills(){'? – songyuanyao

回答

0

的语法去如下:

<return type> <class name>::<function name>(<parameters>); 

而你的情况是:

  • <return type>string *
  • <class name>hunter
  • <function name>theKills
  • <parameters>:无
string * hunter::theKills() { 
    return kills; // you don't need a temporary pointer variable 
} 

保存你使用指针的麻烦,我建议你使用一个std::array,而不是你的C数组string kills[20]

std::array<std::string, 20> kills; 

记住添加const预选赛到每个没有修改你班级任何成员的成员功能。

我猜这是bad practice的使用。

+0

std :: array和C数组有什么区别。 也感谢您的帮助您的冠军语法 – Darko

+0

@Darko看看[这里](https://isocpp.org/wiki/faq/containers)。 –

+0

感谢堆队友,我知道它似乎很小,但你的帮助是非常感激! – Darko