1. Is there a way to go from the sub-classes (one or more layers) to
the base class directly?
A couple of them, in fact. The following code has been tested with G++
4.0.1; other compilers will probably give comparable results.
=====
#include <iostream>
struct Foo {
void print() { std::cout << "Foo" << std::endl; }
};
struct Bar : public Foo {
void print() { std::cout << "Bar" << std::endl; }
};
struct Baz : public Bar {
// This also works
// void print() { Foo::print(); }
void print() { static_cast<Foo* const>(this)->print(); }
};
int main()
{
Baz b;
b.print();
return 0;
}
=====
Please note that in the Baz class, it's usually considered highly
idiosyncratic to use a static_cast to move up in the inheritance
hierarchy. That said, it's entirely legal C++.
Using the scope resolution operator (::) is generally the preferred
solution.
2. What's the sequence of executing constructors function and
destructor function of base class and subclasses?
Top-to-bottom for ctors; bottom-up for dtors.
3. Can a function name starting with $, #, !, -, or a number?
No.