Inner class accessing outer class [duplicate]

2019-01-22 14:01发布

问题:

This question already has an answer here:

  • Inner Class access to Outer Class members 5 answers

I am relatively new to C++, and I have looked a lot for an answer for this thing but I never got a satisfying answer.

Let's say I have a structure called FSM. Eventually in my code, multiple instances of FSM can be created. One of FSM's attributes is int X which is not static, every instance of FSM should have its own value for X.

Now, one of FSM's attributes is another structure submachine which needs to read the value of X like this:

struct FSM
{
  public:
    int x;

    int getX(){return x;}

    struct submachine
    {
        void onentry() {int g = getX();};
    };
};

This gives the following error:

Error: 'FSM::getX' : illegal call of non-static member function

My question is, submachine is a member of FSM, so shouldn't it have access to local instances of all the attributes of FSM? And if not, when we create an instance of FSM, wouldn't we be creating an instance of all its members i.e. submachine? And if so, then why do we need to create an object that onentry() needs?

I am assuming the compiler is correct, so I would also want to know if there's a way to make this work.

NOTE: Unfortunately, the instances of the inner structures (submachine) are instantiated when an event is called and thus I can only define the type, and not instantiate objects for them in FSM.

回答1:

my question is, submachine is a member of FSM, so it should have access to local instances of all the attributes of FSM, no?

No. Unlike in Java, inner class objects don't have an implicit reference to an outer object.

wouldn't we be creating an intance of all its members i.e. submachine?

submachine is a type, not a member variable. If you wanted a member variable, you'd have to do something like this:

struct FSM {
    struct submachine {
        ...
    };

    submachine sm;  // Member variable of type submchine
};

And if you want sm to "see" its parent object, you'll need to pass it explicitly:

struct FSM {
    struct submachine {
        FSM &parent;  // Reference to parent
        submachine(FSM &f) : parent(f) {}  // Initialise reference in constructor
    };

    submachine sm;

    FSM() : sm(*this) {}  // Pass reference to ourself when initialising sm
};

Note that the same principle applies for instances of submachine that aren't member variables. If you want them to be able to access an FSM instance, you'll need to pass a reference to one.

Note also that you could use a pointer rather than a reference. In fact, a pointer offers greater flexibility in many cases.



回答2:

consider that in your example, I can legally write a free function

void foo()
{
    FSM::submachine sub;
    sub.onentry();
}

where there is no FSM instance that sub could refer to.

Either, as Oli says, have the submachine object store a reference to its parent FSM object, or perhaps just pass the value of x directly into onentry (it isn't clear how it gets invoked).


From a quick look at the Boost.MSM docs I found this note on non-default-constructed submachines.

It's pretty ugly, I don't understand the backend enough to paraphrase it here, and the literal code won't make enough sense in isolation to be worth pasting.

The example code linked from there also shows the submachine's entry method with the following signature:

template <class Event,class FSM> void on_entry(Event const&,FSM& );

if that's accurate, you can either store a pointer to your outer state machine on_entry, or extract the value of x there and record it in the submachine.



回答3:

Note that declaring struct submachine only defines the type; it does not actually create a field in the class of that type.

You would need one of the following:

struct submachine mysub; // creates a field after the class is defined

or

struct submachine
{
  . . .
} mysub; // creates the field "mysub" also, as the structure is being defined

This makes mysub the field, and you then access it in the same way that you'd access x.

The definition of submachine needs to include a specific FSM (e.g. a pointer field FSM*, and probably a constructor like submachine(FSM* fsm): fsm_(fsm) {} to initialize it) so that you can say fsm_->getX() to access a certain X value.



回答4:

I am only making a guess at what you want to do, but if my guess is correct, you might be thinking of something like the below.

struct FSM_Base {
    int x;

    struct submachine1;
    struct submachine2;

    FSM_Base () : x(0) {}
    virtual ~FSM_Base () {}
};

struct FSM_Base::submachine1 : virtual public FSM_Base {
    void oneentry () { int g = x; }
};

struct FSM_Base::submachine2 : virtual public FSM_Base {
    void oneentry () { int g = x; }
};

struct FSM : public FSM_Base::submachine1,
             public FSM_Base::submachine2 {
    FSM_Base::submachine1 * sub1 () { return this; }
    FSM_Base::submachine2 * sub2 () { return this; }
};


标签: c++ structure