So I have one main file, and two classes: A display Class, and a job class.
What I want to achieve, is for the main class to be able to call the display class, as well as interface with the job class, BUT I also want the Job Class to be able to call methods from the Display class and send parameters to the display class as well.
I have tried multiple ways of fixing my current problem, but I have not been able to accomplish what I am wanting, I have heard about namespaces, but I am unfamiliar with them and am not sure if this is what I need.
I have also tried to pass the Job/Display objects from main, but that has not worked with what I want to do since in my header I already end up defining a new object.
Here is some example code of what I want to achieve (Please ignore simple compiler errors/This is just example code, and I am not going to post my entire project because that would be way to long/Ignore headers):
Main.cpp
int main(){
Display display;
Job job;
job.init();
display.test();
return 0;
}
Display.cpp
void Display::test(){
std::cout << "testing.." << std::endl;
}
void Display::test2(std::string ttt){
Job job; //Do not want to create a whole new object here
std::cout << "testing3333...." << job.getThree() << std::endl;
std::cout << "testing2222...." << ttt << std::endl;
}
Job.cpp
void Job::init(){
Display disp2; //I do not want to create a whole new object here, but I can't fix this
disp2.test2("from Job");
}
std::string Job::getThree(){
return "test3";
}
Job.h
class Job{
private:
Display disp; // Do not want a new object here as well
public:
void init();
std::string getThree();
};
You need to pass a
Jobpointer to theDisplay, and vice versa, so they know about each other, eg:Main:
Display.h:
Display.cpp:
Job.h:
Job.cpp:
Given the requirements you mentioned, the
Displaydoes not really need to remember theJob, only theJobneeds to remember theDisplay, so you could do something like this as an alternative:Main:
Display.h:
Display.cpp:
Job.h:
Job.cpp: