Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于类中char*函数成员函数的问题 我正试着从Udacity那里学习C++。如果我的理解正确与否,我需要一些澄清。代码如下_C++_Arrays_Class_Pointers - Fatal编程技术网

关于类中char*函数成员函数的问题 我正试着从Udacity那里学习C++。如果我的理解正确与否,我需要一些澄清。代码如下

关于类中char*函数成员函数的问题 我正试着从Udacity那里学习C++。如果我的理解正确与否,我需要一些澄清。代码如下,c++,arrays,class,pointers,C++,Arrays,Class,Pointers,Tictaoe游戏: class Board {//the class tracks the game and the winner char positionsSelected[16]; char winner; public: //Constructor Board(); int setPosition(int gridNumber, char user); char* getPositions(void); }; char* ge

Tictaoe游戏:

class Board
{//the class tracks the game and the winner
    char positionsSelected[16];
    char winner;
    
public:
    //Constructor
    Board();
    int setPosition(int gridNumber, char user);
    char* getPositions(void);
};

char* getPositions(void)
    {//return all the positions on the board
        return positionsSelected;
    }
如果您看一下,成员函数
char*getPositions(void)
是在类中声明的

我知道假设可能很糟糕,但这就是我的思考过程和问题

  • 因为您基本上是在读取一系列字符,所以它必须是
    char*

  • 为什么必须使用
    getPositions(void)
    的参数?这是否可以与空括号()相同

  • 如果函数原型是char*getPositions(),这是否意味着它返回指针?我可以假设
    char*getPosition(void)
    的返回值指向
    char数组(positionsSelected)

  • 如果我的假设是错误的。我可以放这样的东西吗

  • 如有任何建议或解释,将不胜感激

  • 因为你基本上是在读一系列字符,所以它必须是char*
  • 最好是选择成员
    std::string位置。然后可以从方法返回
    std::string const&
    。这既安全又简单易用

  • 为什么getPositions(void)的参数必须为空?这是否可以与空括号()相同
  • 由于baggage from C,这两种语法都可以接受,它们可以表示不同的含义:

    • void foo(void)明确指定不接受任何参数
    • void foo()不指定接受哪些参数
    在C++中,它们都是相同的:没有接受任何参数。空参数是首选语法(
    char*getPositions();

  • 如果函数原型是char*getPositions(),是否意味着它返回指针?我可以假设char*getPosition(void)的返回值指向char数组(positionsSelected)

  • 是的,你可以做这个假设。数组隐式地衰减为指向第一个元素的指针。

    不相关:知道赢家并不是我任务一个Tic-Tac-Toe游戏板的责任。对我来说,
    棋盘
    应该代表棋盘,并且幸福地不知道游戏逻辑。请注意,通过返回
    位置selected
    ,在没有守卫的情况下,
    getPositions
    的调用者可以做任何他们想做的事情。与其将整个游戏板公开给公众审查,不如使用
    char get_position(intx,inty)
    这样的函数,让用户一次只能看到一个角色。这样,游戏板的真正本质仍然存在。你可以完全重写棋盘的内部,没有人会知道它现在将棋盘存储在一团神奇的仙尘或其他东西中。“最好将成员
    std::string positions选中;
    。然后你可以从你的方法返回
    std::string const&
    。这既安全又简单。”-或者,
    std::array
    而不是
    std::string
    ,这样您就不会有动态内存分配的开销。
    char* getPositions(void){
        char* pointers;
        pointers = positionSelected;
        return pointers;
    }