C++ Call member function from other class

2019-09-19 11:31发布

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

2条回答
地球回转人心会变
2楼-- · 2019-09-19 12:02

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++.

查看更多
Fickle 薄情
3楼-- · 2019-09-19 12:18

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...

查看更多
登录 后发表回答