C++ Call member function from other class

情到浓时终转凉″ 提交于 2019-12-13 08:54:53

问题


I have this code. Just 2 small classes and main.

----------MAIN.CPP----------

#include <iostream>

#include "Calendar.h"
#include "Transaction.h"

using namespace std;

int main() {

    cout << "Inicializuji" << endl;

    double Time = 0.0;
    Calendar calendar;

    cout << "Vkladam uvodni udalost" << endl;

    calendar.calendarPush(Time, 1, &Transaction::event1);
    calendar.calendarRun();

}

----------CALENDAR.H----------

#include <iostream>
#include <queue>

using namespace std;

class Calendar;
class Transaction;

typedef void (Transaction::*eventPointer)();

struct activationRecord {
        double Time;
        int Priority;
        eventPointer activationEvent;
};


class Calendar {

private:
        std::priority_queue<activationRecord> activationCalendar;

public:
        bool calendarEmpty();

        void calendarPush(double, int, eventPointer);

        activationRecord calendarTop();
        void calendarPop();

        void calendarRun();
};


----------CALENDAR.CPP-----------

#include "Calendar.h"
#include "Transaction.h"


bool Calendar::calendarEmpty() {

    return activationCalendar.empty();
}

void Calendar::calendarPush(double Time, int Priority, eventPointer event) {

    activationRecord record;

    record.Time = Time;
    record.Priority = Priority;
    record.activationEvent = event;

    activationCalendar.push(record);
}

activationRecord Calendar::calendarTop() {

    return activationCalendar.top();
}

void Calendar::calendarPop() {

    activationCalendar.pop();
}


void Calendar::calendarRun() {

    Transaction transaction;

    activationRecord record;

    while(!calendarEmpty()) {
        record = calendarTop();
        calendarPop();

        (transaction.*record.activationEvent)();

    }
}


bool operator < (const activationRecord & a, const activationRecord & b) {
    return a.Time > b.Time;
}

----------TRANSACTION.H----------

#include <iostream>

using namespace std;

class Transaction;
class Calendar;

class Transaction {

public:
    void event1();
    void event2();
};


----------TRANSACTION.CPP-----------

#include "Transaction.h"
#include "Calendar.h"

using namespace std;

void Transaction::event1() {

    cout << "event1" << endl;

    calendar.calendarPush(1, 1, &Transaction::event2);

}

void Transaction::event2() {

    cout << "event2" << endl;
}   

In brief description, what I have so far is class Calendar which is suppsed to hold priority queue activationCalendar which consists of records of type struct activationRecord

typedef void (Transaction::*eventPointer)();

struct activationRecord {
        double Time;
        int Priority;
        eventPointer activationEvent;
};

and couple of methods operating with the priority queue.

What I want to do in main is to put the first entry into the priority queue by calling

calendar.calendarPush(Time, 1, &Transaction::event1);

which went pretty well. But here comes what I got stuck with. Then I need to call

calendar.calendarRun();

which takes the first entry out from the activationCalendar and calls the pointer to the method it contains, does whatever the method is supposed to do and than within its body push (plan) next record into the activationCalendar.

I tried to let event1 push event2 into the callendar but obviously unsuccesfuly as I dont have the object to call calendarPush from in Transaction class.

void Transaction::event1() {

    cout << "event1" << endl;

    calendar.calendarPush(1, 1, &Transaction::event2);

}

Is there any way how to get the calendar object I defined in main() there (to class Transaction).

Thank You


回答1:


Define it globally (outside the main() function) and then use

extern Calendar calendar;

at the top of your Transaction class header, or wherever else you want to access it.

However, there are probably better ways than this to achieve whatever you're trying to do - it's rare that globals like this are needed in C++.




回答2:


Why don't you pass it as a reference?

void Transaction::event1(Calendar &cal) {

    cout << "event1" << endl;

    cal.calendarPush(1, 1, &Transaction::event2);

}

EDIT: sorry for the ress, the question just popped up on the sidebar so I thought it would be recent...



来源:https://stackoverflow.com/questions/20317224/c-call-member-function-from-other-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!