using a member function of a class in another class and relate it to an object in int main {}

I am writing a program which is using SDL library.

I have two different classes which one of them is Timer Class and the other is EventHandling Class.

I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.

Lets have it as a sample code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Timer
{
    private:
        int x;

    public:
        Timer();
        get_X();
        start_X();
};

class EventHandling
{
    private:
        int y
	
    public:
    EventHandling();
    void handle_Y();
    void move_Y();
};

void handle_Y()
{
  start_X();
  ...
}
void move_Y()
{
  get_X();
}



int main( int argc, char* args[] )
{
//Initialize
//Load the files

Timer Mytimer;
...
}


You will have to give your EventHandling class a pointer or reference to the Timer object. Once you have that pointer/reference, you can call whatever member functions you need.


Or.... maybe have EventHandling own its own timer rather than having it defined in main? Does main need access to the timer?
Yeah, main{ } need to have access to Timer

You mean I need to have like this????

class *Timer
{
...
}

and then have for example:

EventHandling::handle_Y()
{
& Timer::get_X();
}

How the member functions of Timer class should be addressed??
Like
& Timer::get_X();
Last edited on
No you wouldn't modify the Timer class... you'd probably modify the EventHandler class:

1
2
3
4
5
6
7
class EventHandling
{
    private:
        int y
        Timer* timer; // <- give it a pointer to main's timer

// then in EventHandling you can do 'timer->whatever()' to access the timer. 


main would then have to give the EventHandling object a pointer to the timer:

1
2
3
4
5
6
int main()
{
    Timer maintimer;
    EventHandling events;

    events.setTimer( &maintimer );  // <- give it a pointer to maintimer 
Thanks a lot, It was a nice hint. I could manage to use your solution in my program.

Just one more question. lets consider I have these two classes with their member functions.
Now I have an independent function (not in any class) that I have defined outside of main {} and I need to use some EventHandling member functions in that independent function. but like before I am in need of an object of EventHandling in int main{} to have a better control on my program and making it work in parallel.
How and where can I introduce an instance of EventHandling in that function and then by use of dereference in int main {} I can relate the object of EventHandling to its used member functions in that independent function???
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Timer { ... }
class EventHandling
{
    private:
        int y
	
    public:
    Timer* timer; 
    EventHandling();
    void handle_Y();
};
// I used in EventHandling 'timer->whatever()' to access the timer like you said

void RunFunction(Timer *GameTimer, (N.B)
{
    EventHandling::void handle_Y();

}

// N.B =some thing like "EventHandling * Myevent" 
// I need to pass an instance  of EventHandling as an argument to void
// RunFunction () that after calling function "RunFunction" in main I can pass 
// the defined object of "EventHandling events;" as an argument to this
// function to relate it with the member functions of EventHandling class that 
// have been used in RunFunction.


int main()
{

Timer maintimer;
EventHandling events;

events.setTimer( &maintimer ); 

RunFunction( &maintimer , &events)

return 0;

}
Last edited on
Topic archived. No new replies allowed.