引用init两次cpp

时间:2015-12-10 09:50:04

标签: c++ constructor reference

您好,我试图初始化一个通过引用传递成员的对象,

    class Machine: public sc::state_machine<Machine, Control>
{
        public: 
        FIFOmidi& _midififo;

        public:
        Machine(FIFOmidi& m_fifo);

};

Machine::Machine(FIFOmidi& m_fifo):_midififo(m_fifo){}

然后我想在一个Object构造函数中初始化它,它具有相同的引用对象:

    class StateMachine{

    private: 

    FIFOmidi& _midififo;
    Machine myMachine;

    public: 

    StateMachine(FIFOd3data& d_fifo, FIFOmidi& m_fifo);
};

            StateMachine::StateMachine(FIFOd3data& d_fifo, FIFOmidi& m_fifo )
        :_sharedfifo(d_fifo),_midififo(m_fifo){}

StateMachine有一个类型为Machine的对象(我不使用继承)

然后如何从Object StateMachine初始化一个对象机?

因为当我声明一个Machine Object的实例时,编译器会问我引用,因为它没有...

Thanx !!

1 个答案:

答案 0 :(得分:1)

只需将引用作为参数提供给构造函数:

StateMachine::StateMachine(FIFOd3data& d_fifo, FIFOmidi& m_fifo )
  : _sharedfifo(d_fifo), _midififo(m_fifo), myMachine(m_fifo) {}