-1

#include <iostream>

class  A
{

public:
    A(/* args */){};
    virtual void p(){std::cout << "A\n";}
};

class  B : public A
{

public:
    B(/* args */){};
};

class  C : public B
{

public:
    C(/* args */){};
    void p() override{std::cout << "C\n";}
};

int main(int argc, char const *argv[])
{
    B test = C();
    C test2 = C();


    test.p();
    test2.p();
    return 0;
}

Curently it Prints: A C

I would like to save C in B form like the 'test' variable; but call the override version of the function, now it's calling the base one.

1 Answers1

-1

Found this solution.

int main(int argc, char const *argv[])
{
    B *test = new C();
    C test2 = C();


    test->p();
    test2.p();
    return 0;
}
  • 1
    This is correct, but it needs explanation of how and why it works otherwise all most folks will get from it is a chance to level up their copy-and-paste skills. – user4581301 Nov 19 '21 at 21:02