C ++从其他类调用成员函数 [英] C++ Call member function from other class

查看:127
本文介绍了C ++从其他类调用成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码。只有2个小班和主。

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;
}   

简单来说,我到目前为止是类Calendar,保持优先级队列activationCalendar,其由类型struct activationRecord的记录组成

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.

我想在主要是通过调用

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();

它从activationCalendar中获取第一个条目,并调用指向它包含的方法的指针,该方法应该做,而不是在其内部push(plan)下一条记录到activationCalendar。

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.

我试图让event1将event2推送到callendar,但显然不成功,因为我没有在Transaction类中调用calendarPush的对象。

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

谢谢

推荐答案

为什么不传递参考?

void Transaction::event1(Calendar &cal) {

    cout << "event1" << endl;

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

}

编辑:对不起,问题刚刚弹出在侧边栏,所以我以为这将是最近的...

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

这篇关于C ++从其他类调用成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆