繁体   English   中英

如何在源文件中实现嵌套类构造函数

[英]How to implement Nested Class Constructor in Source file

我在我的主类中有一个名为cell的嵌套类。 我知道了

class Something{
  class Cell
    {
    public:
        int get_row_Number();
        void set_row_Number(int set);

        char get_position_Letter();
        static void set_position_Letter(char set);

        void set_whohasit(char set);
        char get_whohasit();

        Cell(int row,char letter,char whohasit);

    private:
        char position_Letter;
        int row_Number;
        char whohasit;
    };
};

我想在.cpp文件中实现嵌套类构造函数

Something::Cell Cell(int row,char letter,char whohasit){
    Something::Cell::set_position_Letter(letter);
    Something::Cell::set_row_Number(row);
    Something::Cell::set_whohasit(whohasit);
}

但这是错误的。 我认为正确的将是Something :: Cell :: Something :: Cell,但我认为这也不是真的。

快到了 它很简单:

Something::Cell::Cell(int row,char letter,char whohasit){
    Something::Cell::set_position_Letter(letter);
    Something::Cell::set_row_Number(row);
    Something::Cell::set_whohasit(whohasit);
}

但实际上,我强烈建议您使用初始化程序,而不是构建未初始化的成员,然后分配给它们:

Something::Cell::Cell(int row, char letter, char whohasit)
    :position_Letter(letter)
    ,row_Number(row)
    ,whohasit(whohasit)
{}

你需要将你的内部类公开,并且方法set_Position_Letter不能是静态的,因为char position_Letter不是静态的(这里是标题):

class Something
{
public:
    class Cell {
    public:
        int get_row_Number();
        void set_row_Number(int set);

        char get_position_Letter();
        void set_position_Letter(char set);

        void set_whohasit(char set);
        char get_whohasit();

        Cell(int row,char letter,char whohasit);

    private:
        char position_Letter;
        int row_Number;
        char whohasit;
    };
};

这是cpp:

Something::Cell::Cell(int row, char letter, char whohasit) {
    set_position_Letter(letter);
    set_row_Number(row);
    set_whohasit(whohasit);
}

void Something::Cell::set_position_Letter(char set) {
    this->position_Letter = set;
}

void Something::Cell::set_whohasit(char set) {
    this->whohasit = set;
}

void Something::Cell::set_row_Number(int set) {
    this->row_Number = set;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM