Cześć,
Mam problem zależności pomiędzy dwoma klasami, doczytałem, że najlepiej zrobić refactoring i wydzielić co wspólne do klasy trzeciej, ok. Natomiast będzie z tym więcej roboty niż zrobienie to w taki sposób jak chciałbym poniżej (być może mniej poprawny i niezalecany).
To co jest poniżej działa ale mam pytanie czy to co zrobiłem jest poprawne? Głównie chodzi o fragment przekazania referencji do wskaźnika this do metody hello.
Jeżeli nie, to w jaki sposób zrobić to poprawnie.
#ifndef FOO_H
#define FOO_H
#include <iostream>
#include "Bar.h"
using namespace std;
class Bar;
class Foo {
public:
void run() {
bar->hello(*this);
};
private:
Bar *bar;
};
#endif /* FOO_H */
#ifndef BAR_H
#define BAR_H
#include <iostream>
#include "Foo.h"
using namespace std;
class Foo;
class Bar {
public:
void hello(const Foo &foo) {
cout << "hello Bar" << endl;
....
}
};
#endif /* BAR_H */
#include <iostream>
#include "src/cpp/Foo.h"
using namespace std;
int main(int argc, char** argv) {
Foo foo;
foo.run();
return 0;
}