Unique pointers with derived classes

This may sound confusing, but here goes. So I have two inferfaces, intoLogic and intoView, both of which are abstract classes. Both of them are derived to two classes, Logic and View. Basically, I want to have the class Logic call functions from interface intoView, that View will actually execute. Then I want to have the class View call functions from interface intoLogic, that Logic will actually execute. So basically, View calls a function from intoLogic, that actual Logic will execute and then Logic calls a function from intoView, that actual View will execute. So in code, it looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class intoLogic
{
public:
   virtual void calculate() = 0;
};

class Logic: public intoLogic
{
public:
   Logic();
   void calculate();
private:
   //A (unique) pointer of type intoView that points at View, so that I can
   //call for instance pointertoview->showResult();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class intoView
{
public:
   virtual void showResult() = 0;
};

class View: public intoView
{
public:
   View();
   void showResult();
private:
   //A (unique) pointer of type intoLogic that points at Logic, so that I can
   //call for instance pointertologic->calculate();
};


So how do I create the unique pointers that way? Is it overall possible to have something like

std::unique_ptr<intoView> graphics(new View);

in the private of Logic, so that I can call graphics->showResult();?

When I try implementing it like that, I get a lot of problems trying to include everything needed in the classes. So how should I solve this? I was thinking about a factory method, where I create a separate module to assign the unique pointers, but is there an easier way?

EDIT: I tried doing it with the factory method, but it will just end up with the program stuck in an infinite loop. I'm out of ideas.
Last edited on
> When I try implementing it like that, I get a lot of problems trying to include everything needed in the classes.
http://www.cplusplus.com/forum/articles/10627/
separate the declaration and definition of the member functions.
only the definition file would need to include logic


> Is it overall possible to have something like
> std::unique_ptr<intoView> graphics(new View);
> in the private of Logic, so that I can call graphics->showResult();?
¿you don't care about the state of the object?
As long as your base class destructor is virtual there would be no problem, but you could as well do
1
2
View graphics;
graphics.showResult();



> it will just end up with the program stuck in an infinite loop
I don't want to play a guessing game.
in order to find out what you did wrong we need to know what you did.
unique pointer implies ownership.

If a Logic owns a View... then a View can't own a Logic because you'll create a never ending chain of objects. If you want the objects to refer to each other, then you'll need a non-ownership pointer (ie: a basic pointer)
Actually I think I have some sort of an idea what the problem is with the infinite loop.

I already have one View-object in main.cpp, since the View actually creates the UI for the program. Eventually when the View creates a pointer to intoLogic, it will create a new object of Logic. So far so good. But then the Logic needs to create a pointer pointing at the View-object, but it will actually dynamically allocate a new instance of View. So there we have an infinite loop. That new instance of View will again create a new Logic and that Logic will again create a new instance of view. I'll have to test this out in practice.
Yes, that is what I said. =P
Topic archived. No new replies allowed.