Question

I want to fill a constexpr table with pointers to call the pointers later. The given example shows only one entry.

I run into two problems:

1) It is not possible for me to find a correct syntax to write a pointer a member class object which is able to initialized to an object of a derived class.

2) I could not use the pointer to call the 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 A 
{   
    public:

        S1 s1; 
        S2 s2; 
};  

class B
{   
    private:
        static constexpr A a{};
        static constexpr State A::*state { &A::S2 }; // < do not work! 

    public:

        void Do() const
        {   
            (a.*state).Do();    // is it possible to have a pointer to any State class to call virtual functions?  
        }   
};  

constexpr A B::a;


int main()
{   
    B b;
    b.Do();
    return 0;
}   
Was it helpful?

Solution

I think the issue is an oversite in the standard. There's no reason why:

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

shouldn't work, but the wording to allow it is missing in the standard. There are a number of work-arounds: the most obvious would be to have an accessor function returning a State* for each member variable, and use a pointer to the function:

class A
{
public:
    S1 s1;
    State* getS1() { return &s1; }
    S1 s2;
    State* getS2() { return &s2; }
};

class B
{
    static A a;
    static State* (A::*getState)();
public:
    void Do() const
    {
        (a.*getState)()->Do();
    }
}

State* (A::* B::getState)() = &A::getS1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top