You can't make a nested class virtual as you do with member functions. A virtual member function is a function that the base class expects derived classes to redefine. You can't however redefine classes once they are defined. Well, not the same way. Read on...
A nested class is not a normal member of your class. Remember it simply defines an object. It doesn't instantiate one. In other words it defines a new type inside the enclosing class scope. What is now left to know is the inheritance rules for nested classes. Take a look at the following example.
Code:
class Base {
public:
class Nested {
public:
int valNested;
};
int val Base;
};
class Derived: public Base {
public:
int valDerived;
};
The class Nested is defined inside Base, however, it is not instantiated as a member of this class. However, since it is defined inside the public access control label, it can be used by users of the Base class.
The scope rules for nested classes state that a nested class scope is that of its enclosing class. That means Nested scope is Base. In C++ syntax, Base::Nested. So the bellow code is correct:
Code:
int main() {
Base foo; // declares an instance of the class Base
Base::Nested bar; // declares an instance of the class Nested
}
So, what about the derived class? Derived classes are known to be composed of two parts; Their own non-static members and the non-static members of their base class. But the keyword here is members. It just so happens a nested class is not a normal member of a class. It simply defines a new type inside that class scope.
However, I did say earlier the nested class is defined inside the scope of the base class. Can it be this definition is carried on through a derived class?
Code:
int main() {
Derived foo; // declares an instance of the derived class
Derived::Nested bar; // declares an instance of the class Nested
}
Yes, it can! So, all in all, you don't need to define again your nested class inside the derived class. The definition inside Base will carry over to its derived classes and follows the same rules concerning private, protected or public members.
But lets try and do what you were trying to do and define Nested inside the derived class too:
Code:
class Derived: public Base {
public:
class Nested {
public:
int valNested;
};
int valDerived;
};
I changed Derived to look like the above. We now have the "same" class being defined twice. Once inside Base and another inside Derived. Notice I put same inside quotes... The thing is that they aren't the same. They are two different classes.
Remember I said nested classes are defined inside the scope of the enclosing class. As such, the base class defines one nested class named Nested. While the derived class defines another nested class also named Nested. There isn't any compiler error because both Nesteds belong to different namespaces. And even being Derived a class that inherits from Base, there is still no conflict.
Look:
Code:
int main()
{
Derived::Base::Nested foo;
Derived::Nested bar;
}
On the first line I'm created an instance of Nested calling on the base portion of the Derived class (Base::Nested). On the second line I'm creating an instance Derived::Nested. Both Nested classes are different. They have the same name but they could have been defined completely different from each other.
So, even if you thought you were redefining a class by repeating the definition inside the derived class, you weren't. You would be creating an entirely new class under the Derived scope, and effectively shadowing the definition of base (although still be able to access it, as demonstrated above).