Is there any way of avoiding using virtual methods in a particular custom class inheritance structure?


I have a set of classes similar to the ones shown below. However, I would like to allow users to derive from them without having to modify the base classes with additional virtual methods. Is there a way of modifying the code without having to declare virtual int GetT(void){return 0;}; in class A?

To explain the problem further, I would like to allow users to extend both A and B (or Aext and Bext) with their own methods, which are not defined as virtual methods in A and B. I am looking for a casting method that would allow casting a pointer to base class to a pointer of a derived class. In essence, I would like to use something similar to dynamic_cast<Aext*>(Trajectory[i]) if Trajectory[i] contains a pointer to the derived class, i.e. Aext.

class A {
    int a;
    A(int& ca) {a=ca;}
    virtual int GetT(void){return 0;};

class B{
    boost::ptr_vector<A> Trajectory;
    void PushBackStatePoint(int& ca);

void B::PushBackStatePoint(int& ca) {
    Trajectory.push_back(new A(ca));

class Aext: public A {
    int t;
    Aext(int& ca, int& ct) : A(ca) {t=ct;}
    virtual int GetT(void) {return t;}

class Bext: public B {
    void PushBackStatePoint(int& ca, int &ct);
    int GetFirstElement(void){return Trajectory[0].GetT();}

void Bext::PushBackStatePoint(int& ca, int &ct) {
    Trajectory.push_back(new Aext(ca,ct));


int main(){     
    Bext MyB;
    int a(5);
    int t(3);
    std::cout << MyB.GetFirstElement();

This video can help you solving your question :)
By: admin