Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

C++ Call member function from other class

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...