I would like to keep the code readable by writing the actual code of a nested class outside the main class, Is it possible, and how ?
class AA{
//random code
class BB : public CC <double> {
// very long code
};
// random code
};
I would like to write something like :
class AA{
//random code
//<declaration of class BB>
// random code
};
class BB : public CC <double>{
// very long code
};
and the BB class should only be accessible within the AA 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. They are accessed using the enclosing class name. To instantiate an inner class, you must first instantiate the outer class.
Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. Following is the program to create an inner class and access it.
Using the nested class will make your code more readable and provide better encapsulation. Non-static nested classes (inner classes) have access to other members of the outer/enclosing class, even if they are declared private.
If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.
class A {
class B;
};
class A::B {
// ...
};
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