Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Nested Class Constructor in Source file

I have a nested class called cell in my main class something. I c

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;
    };
};

I wanna implement nested class constructor in .cpp file

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);
}

But it is wrong. I assumed correct would be Something::Cell::Something::Cell at first but i don't think thats true either.

like image 752
paypaytr Avatar asked Oct 10 '17 08:10

paypaytr


People also ask

Can nested class have constructor?

They have access to both static and non-static members in the enclosing context. They can only define instance members. They're the only type of nested classes that cannot define constructors or extend/implement other classes or interfaces.

How do you reference a nested class method?

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

How do you cite a nested class in Java?

In order to create an instance of the Nested class you must reference it by prefixing it with the Outer class name, like this: Outer. Nested instance = new Outer. Nested();

What is nested class what is its use explain with example?

A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed. For example, program 1 compiles without any error and program 2 fails in compilation.


2 Answers

You are almost there. It's as simple as:

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);
}

But actually, I would strongly recommend you use initializers, rather than constructing the members uninitialized, and then assigning to them:

Something::Cell::Cell(int row, char letter, char whohasit)
    :position_Letter(letter)
    ,row_Number(row)
    ,whohasit(whohasit)
{}
like image 117
Martin Bonner supports Monica Avatar answered Oct 20 '22 18:10

Martin Bonner supports Monica


You need to make your inner class public, and the method set_Position_Letter cannot be static, because char position_Letter is not static (here is the header):

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;
    };
};

This is the 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;
}
like image 36
Danilo Carrabino Avatar answered Oct 20 '22 18:10

Danilo Carrabino