c++ - pointer to member and using virtual functions -


i want fill constexpr table pointers call pointers later. given example shows 1 entry.

i run 2 problems:

1) not possible me find correct syntax write pointer member class object able initialized object of derived class.

2) not use pointer call virtual functions.

#include <iostream>  using namespace std;  class state { public: virtual void do() const {} }; class s1: public state { public: virtual void do() const { cout << "s1 do" << endl; } }; class s2: public state { public: virtual void do() const { cout << "s2 do" << endl; } };  class  {        public:          s1 s1;          s2 s2;  };    class b {        private:         static constexpr a{};         static constexpr state a::*state { &a::s2 }; // < not work!       public:          void do() const         {                (a.*state).do();    // possible have pointer state class call virtual functions?           }    };    constexpr b::a;   int main() {        b b;     b.do();     return 0; }    

i think issue oversite in standard. there's no reason why:

state a::*state = static_cast<state a::*>( &a::s1 ); 

shouldn't work, wording allow missing in standard. there number of work-arounds: obvious have accessor function returning state* each member variable, , use pointer function:

class { public:     s1 s1;     state* gets1() { return &s1; }     s1 s2;     state* gets2() { return &s2; } };  class b {     static a;     static state* (a::*getstate)(); public:     void do() const     {         (a.*getstate)()->do();     } }  state* (a::* b::getstate)() = &a::gets1; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -