Pointers always indicate the beginning of the object, and the beginning of the object for all heirs is the beginning of the object of the base class (the base class can have an array, structure, etc.) in its representation.
Therefore, it is impossible to refer to the own methods of the heir object through the base class pointer. The base class has no idea how many and what heirs it may appear. This enables the independence of the already written code (classes) from the newly created ..
If the base class is abstract, it’s best to simply call one of the virtual functions of the base class in your function. Then everything will be as you wish. For example:
using namespace std; class abst { public: virtual ~abst() = default; virtual void f() = 0; }; template <typename T> class A : public abst { public: //... void fun() { cout << "show " << T() << endl; } void f() { fun(); } }; template <typename T> class B : public A<T> { public: void fun() { cout << "creat " << T() << endl; A<T>::fun(); cout << endl; } void f() { fun(); } }; struct Some_type { const string s; Some_type() : s("Some_type ") {} friend std::ostream& operator << (ostream& os, const Some_type& t) { os << ts; return os; } }; int main() { list<abst*> l; A<int> a; B<float> b; A<string> c; B<Some_type> d; d.fun(); l.push_back(&a); l.push_back(&b); l.push_back(&c); l.push_back(&d); for (const auto& p : l) // p->fun(); // так не можете p->f(); // только так return 0; }
And there is no need for a new inheritance ..
fun- virtual, inherited from an abstract class? Or arbitrary specific classA? - Harry*l.begin()is the same asl.front(). - bipll am