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.
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.
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.
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();
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.
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)
{}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With