Why the next example prints "" and what must change for this to print "1" when i expected ?
#include <iostream> struct base base() virtual ~base() struct derived : public base int primary(void)
Because 'base' is built first and has not "matured" right into a 'derived' yet. It cannot call techniques with an object if this can't guarantee the object has already been correctly initialized.
Whenever a derived object has been built, prior to the body from the derived class constructor is known as the bottom class constructor must complete. Prior to the derived class constructor is known as the dynamic kind of the item being built is really a base class instance and never a derived class instance. Because of this, whenever you call an online function from the constructor, just the base class virtual function overrides could be known as.
Really, there's a method to understand this behavior. "Every condition in software could be solved with an amount of indirection."
/* Disclaimer: I've not done C++ in lots of several weeks now, there can be a couple of syntax errors in some places. */ class parent protected: struct parent_virtual parent( const parent_virtual&lifier obj ) class child : public parent public: child( ) : parent( child_virtual( ) )
You shouldn't call the virtual techniques from constructor. Rather you are able to give them a call after construction of object.
Your code could be re writtern the following
struct base base() virtual ~base() struct derived : public base int primary(void)
The overall rule is that you simply don't call an online function from the constructor.