我正在开发一个程序,但在涉及类时无法理解使用指针。我知道我必须为指针分配内存,new
并且在不使用类时对此很好。不过,我找不到一个简单的教程来解释如何执行此特定任务。有人可以给我一些帮助吗?这是我到目前为止所做的相关片段,但它正在输出随机字符:
"Hangman.c"
{
class Hangman
{
public:
...
char* remainingLetters();
Hangman()
{
char* remaining=new char[26];
}
~Hangman();
private:
char* remaining;
}
"Hangman.cpp"
{
...
char* Hangman::remainingLetters()
{
...does task to find remaining letters;
return remaining;
}
ostream& operator<< (ostream &out, Hangman &game)
{
out << "Letters remaining are: " << game.remaining <<endl
return out;
}
}
"main.cpp"
{
...
cout << game;
...
}