-4

I've got helped a lot in here.

this is my first time to ask a question. ahead, sorry for my bad English.

I made two classes which is singleton for a project. and its class needs to use other class's functions.

One is app class managing overall app's function and the other one is menu class, which I am trying to add go back function by using stack.

app needs menu, and menu needs app to execute app function when you select a menu numbers.

but error shows up and I have no idea how should i correct it ;_; thank you for your help

this is app.h & app.cpp file

   #include <iostream>
    #include "Menu.h"
    using namespace std;

    class App
    {
        static App app;
        App();
    public:
        static App* GetAppInstance();
        Menu* getMenuInstance();
    };

   #include "App.h"
    #include <iostream>
    using namespace std;

    App App::app;
    App::App()
    {
        cout << "App" << endl;
    }

    App * App::GetAppInstance()
    {
        return &app;
    }
    Menu* App::getMenuInstance()
    {
        return Menu::getMenuInstance();
    }

and this is Menu.cpp and Menu. h file

#include <iostream>
using namespace std;
class App;
class Menu
{
    static Menu menu;
    Menu();
public:
    static Menu* getMenuInstance();
    App* getAppInstance();
};

#include "Menu.h"
#include <iostream>
using namespace std;
class App;
Menu::Menu()
{
    cout << "메뉴" << endl;
}

Menu * Menu::getMenuInstance()
{
return &menu;
}

App* Menu::getAppInstance()
{
    return App::GetAppInstance();
}

Menu Menu::menu
  • 2
    Your design is seriously flawed. 1. What does _Singleton_ say? You should only have one overall. 2. That can be completely solved without any Singletons at all. 3. Regarding `friend` read [here](https://stackoverflow.com/questions/27492132/how-can-i-remove-refactor-a-friend-dependency-declaration-properly) please. – πάντα ῥεῖ Jun 25 '17 at 13:20
  • 1
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Jun 25 '17 at 13:21
  • Another serious flaw in your design is [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Do yourself a favor and completely forget that this exists in C++. If this is what your teacher is using as an example, find a better C++ teacher. – Sam Varshavchik Jun 25 '17 at 13:41

1 Answers1

1

Having interdependent classes is bad as you can't test one without the other. This built-in inability to test is often taken as a reason not to test rather than a reason to refactor the setup to break the dependency cycle. As with interdependent classes singletons are also an anti-pattern as they effectively inhibit testing of different setups. While it is short-term easier to just access a global variable mid to long term it is a massive headache and other approaches, e.g., explicitly passing a suitable everywhere it is needed actually makes things easier.

There are many ways to break dependency cycles (John Lakos's Large Scale C++ discusses those in detail; be warned, though: while the discussion on dependencies is reasonable, a lot of what is said in this book felt rather dated when the book was released 20+ years ago...). The easiest for your setup is probably to split the menu functionality into an abstraction providing the menu interface and its actual implementation:

  • The menu abstraction is just a base class which probably doesn't do anything or does very little, certainly not depending on an application entity.
  • The application class would travel in terms of the menu abstraction, having a dependency on the [abstract?] menu base class but not on the actual menu implementation.
  • The menu implementation derives from the menu abstraction and uses the application as appropriate. The menu implementation would have a dependency on both the menu abstraction and the application but there is no cyclic dependency.
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380