   Is my virtual table broken?
  HiIm in a situation where I want to let a class use its base class version of a virtual function, but at the same time I need the virtualness to continue down the hierarchy.My test program might clearify what I mean...#include <iostream>using namespace std;struct A { A(int value) : v(value) {}virtual ~A() {}int v; virtual void print() const { cout << v << endl; }};struct B : public A{ B(int value) : A(value) {}virtual ~B() {}//virtual void print() const { cout << v << endl; }};struct C : public B{ C(int value) : B(value) {}virtual ~C() {}virtual void print() const { cout << v << endl; }};int main(){A a(1);B b(2);C c(3);a.print();b.print();c.print();cin.get();return 0;}If I comment out B's print function, The output is still  1 2 3 So it seems that B uses A's print routine without braking the virtual chain down to C.What I wonder is if this is a viable option.If the answer is no, I suspect that B should always implement its virtual functions, even if they are exactly the same as A's.If thats the case, my question would be if doing it like this is good:void B::print() const { A::print(); }Thanks
  Try this (again commenting and uncommenting B's print) :#include <iostream>using namespace std;struct A { A(int value) : v(value) {}virtual ~A() {}int v; virtual void print() const { cout << "A::print() " << v << endl; }};struct B : public A{ B(int value) : A(value) {}virtual ~B() {}virtual void print() const { cout << "B::print() " << cout << v << endl; }};struct C : public B{ C(int value) : B(value) {}virtual ~C() {}virtual void print() const { cout << "C::print() " << cout << v << endl; }};int main(){A a(1);B b(2);C c(3);a.print();b.print();c.print();cin.get();return 0;}You problem is that B's consturctor makes v = 2, so A::Print() prints the value 2 anyway. Having B::print() doesnt change this.   ;  Your test doesn't tell you anything, all 3 print functions are exactly the same so they'll do exactly the same thing (ie. print v).  Because of this if you pass 1, 2 and 3 to instances of A B C, you'll be printing 1 2 3.  Here's your test modified so that it gives you a better idea of how virtual functions work:#include <iostream>using namespace std;struct A { virtual void print() const { cout << 'A' << endl; }};struct B : public A{ virtual void print() const { cout << 'B' << endl; }};struct C : public B{ virtual void print() const { cout << 'C' << endl; }};int main(){A a;B b;C c;a.print();b.print();c.print();cin.get();return 0;}Comment out the print() functions of B and C in the 4 different combinations to see what effect it has.EDIT: too slow.....   ;Quote:Original post by joanusdmentiaYour test doesn't tell you anything, all 3 print functions are exactly the same so they'll do exactly the same thing (ie. print v).  Because of this if you pass 1, 2 and 3 to instances of A B C, you'll be printing 1 2 3.  Here's your test modified so that it gives you a better idea of how virtual functions work:*** Source Snippet Removed ***Comment out the print() functions of B and C in the 4 different combinations to see what effect it has.Yes my first test program did it like you guys do.The output was 1 1 3. (After out comment B's print)The thing is that this is what I want, but I dont know if it is bad or not.   ;  Okay, reread your first post a little, the code you posted confused me.If I understand you correctly, then yes this "void B::print() const { A::print(); }" will work as you want it to.You want to use the imlementation of a base classes virtual function in a sub-class, while still allowing the function to be changed yet again in a sub-sub-class, yes?   ;  Yes exactly ^^Ill show you my "real" code for this:class Picture : public Render::Base{protected:Picture(bool transp) : surface(0), transparent(transp) {}public:Picture() : surface(0), transparent(false) {}virtual ~Picture() {}Surface *surface;bool transparent;virtual void draw(Surface* back, const RECT &window) const;virtual void move(int dx, int dy, const RECT &window);virtual bool contain(int x, int y) const { return (x > pos.x && x < pos.x + dim.x && y > pos.y && y < pos.y + dim.y) ? true : false; }};//======================================class Frame : public Picture{public:Frame() : Picture(true), border_size(12) {}virtual ~Frame() {}int border_size;//virtual void draw(Surface* back, const RECT &window);//virtual void move(int dx, int dy, const RECT &window);virtual bool contain(int x, int y) { if(x > pos.x && x < pos.x + border_size && y > pos.y && y < pos.y + dim.y)return true;if(x > pos.x + dim.x - border_size && x < pos.x + dim.x && y > pos.y && y < pos.y + dim.y)return true;if(y > pos.y && y < pos.y + border_size && x > pos.x && x < pos.x + dim.x)return true;if(y > pos.y + dim.y - border_size && y < pos.y + dim.y && x > pos.x && x < pos.x + dim.x)return true;return false;}};This worked as I wanted it to, but the fact that Frame "breaks" the virtual chain worried me.Ill go with the derived class calling its base then =)Thanks again   ;  Is this a better example of what you're wanting?#include <iostream>using namespace std;struct A { A(int value) : v(value) {}virtual ~A() {}int v; virtual void print() const { cout << v << endl; }};struct B : public A{ B(int value) : A(value) {}virtual void print() const { cout << v*10 << endl; }};struct C : public B{ C(int value) : B(value) {}virtual void print() const { C::print(); }};int main(){A a(1);B b(2);C c(3);a.print();b.print();c.print();cin.get();return 0;}btw, no need for the empty virtual destructors in the derived classes.  Having the destructor of the base class (A) virtual is sufficient.EDIT: still too slow [tears]   ;Quote:Original post by pulpfistYes exactly ^^Ill show you my "real" code for this:*** Source Snippet Removed ***This worked as I wanted it to, but the fact that Frame "breaks" the virtual chain worried me.Ill go with the derived class calling its base then =)Thanks againNot sure what you mean by "breaks the virtual chain", but this looks fine to me and is exactly how virtual functions should be used.   ;  pulp, you NEVER need to override a virtual function that JUST calls the base class's version, because that happens automatically - if you write nothing at all.  You only need to write an override if you want a different behavior than the base class.class OriginalBase  {  public:    virtual void Print(ostream &os) const { os << someMember; }  };class RedundantOverride : public OriginalBase  {  public:    // don't do this, it just confuses people reading it    virtual void Print(ostream &os) const { OriginalBase::print(os); }  };class RealOverride : public RedundantOverride  {  public:    virtual void Print(ostream &os) const { RedundantOverride::print(os); os << anotherMember; }  };;[joanusdementa]:Ok you see what I mean. What I want is to skip Frame's draw and move routines since Picture already has some that works.What I meant with break the chain is that the way I see it the virtual functions in the V table point to eachother, so if I skip one in the middle somewhere it could "break" the branching down the V table.I dont know much about this so it might not make much sense.   