Why have virtual static members not been added as a feature of C++? -
i read (for k'th time)
which question simulating virtual static members. question - made c++ standards committe (or bjarne stroustrup before that) not add feature c? known break anything? or impede performance of (even when not used)?
to better illustrate i'm taking on feature definition itself, here code:
// not compile! class base { // pure virtual member - perhaps need indicate somehow virtual static const string towhom; void sayhello() { cout << "hello, " << towhom << "!" << endl; } }; class world : public base { static virtual const string towhom = "the entire world"s; // c++14 literal }; class : public base { static virtual const string towhom = "everybody around"s; }; note: i'm not asking opinion or whether adding these idea, i'm asking history , official considerations.
first, let's @ invalid example of how static virtuals look:
// warning: not compile !!! class base { static virtual string towhom() { return "unknown"; } static void sayhello() { cout << "hello, " << towhom() << "!" << endl; } }; class world : public base { static virtual string towhom() { return "world"; } }; class : public base { static virtual string towhom() { return "everybody"; } }; this let this:
// warning: not work !!! everybody::sayhello(); // prints "hello, everybody!" world::sayhello(); // prints "hello, world!" the problem, however, dispatch not easy implement without changing way static functions called in c++.
recall non-static member functions this parameter implicitly. this parameter carries information virtual functions it. when call static function, however, nothing passed identify current class (i.e. hello vs. everybody in example above). there no implicit arguments, hence no access virtual functions.
going example above, consider happen when base::sayhello calls towhom(). needs have context make decision on of 3 function should called - i.e. base::towhom, world::towhom, or everybody::towhom. not information missing, there no existing mechanism in language on "piggyback" functionality in way similar how pointer virtual functions added data class.
although true invocation mechanism changed, authors of language did not see compelling reasons doing this.
Comments
Post a Comment