У меня есть этот код. Всего 2 маленьких класса и основной.C++ Функция вызова от другого класса
----------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;
}
В кратком описании то, что я до сих пор является класс календарь, который suppsed для хранения приоритета activationCalendar очереди, которая состоит из записей типа структуры activationRecord
typedef void (Transaction::*eventPointer)();
struct activationRecord {
double Time;
int Priority;
eventPointer activationEvent;
};
и пару методов, работающих с приоритетом очередь.
То, что я хочу сделать в главное, чтобы поставить первую запись в приоритетной очереди по телефону
calendar.calendarPush(Time, 1, &Transaction::event1);
который пошел очень хорошо. Но вот что я застрял. Тогда мне нужно позвонить
calendar.calendarRun();
, который принимает первую запись из activationCalendar и вызывает указатель на метод она содержит, делает то, что метод должен делать и чем в пределах его тело толчка (план) следующая запись в календарь активации.
Я попытался позволить event1 нажать event2 в callendar, но явно неуспешно, поскольку у меня нет объекта для вызова calendarPush из класса Transaction.
void Transaction::event1() {
cout << "event1" << endl;
calendar.calendarPush(1, 1, &Transaction::event2);
}
Есть ли способ получить объект календаря, который я определил в main() там (для класса Transaction).
Спасибо
Да, это именно то, что я хотел бы избежать. Я просто не могу понять, как это сделать. – jankoritak
Альтернативой может быть реализация singleton или наличие каждого из ваших других классов, полученных из родительского класса, который затем создается в основном или любое количество альтернативных шаблонов в ООП. – Riot