Interdependent Objects

I am writing a flowchart simulator and have the classes LinkCell and PassCell.
The problem is when the flowchart should loop in a condition.

I first tried this, but the obvious "not defined error appears":

1
2
PassCell wait_year( "Wait another year", &over_eighteen );
LinkCell over_eighteen( "Are they both over 18", &next_step, &wait_year;)


I was wondering if I could do something like this:

 
LinkCell over_eighteen( "Are they both over 18", &next_step, &(PassCell wait_year( "Wait another year", &over_eighteen )));


This specific example doesn't work though...
Last edited on
That is what happens when you've got a 1 to 1 relationship. It can't be instantiated (well actually you can, but it's ugly)
Either both classes are the same thing, or one of the cardinalities can be 0
well actually you can, but it's ugly


What is that way? :)

Just wondering, because the functionality I need to implement uses this principle.
Maybe some sort of temporary/virtual definition…
Passing this in the constructor. The other objects can't use it because it is in an invalid state, but they can store the reference. And you can't use composition, so you need to create them dynamically, but as you can't return anything from a constructor, ¿how will you access it later?
1
2
3
4
5
6
PassCell::PassCell(){
  link = new LinkCell(this); //the constructor will just store that pointer. It shuldn't use it yet.
}

PassCell wait_year;
LinkCell *over_eighteen = wait_year.link; //it needs to be deleted 
Awful.

because the functionality I need to implement uses this principle.
¿what principle?
Maybe some sort of temporary/virtual definition…
I'm not following you.
Maybe some sort of temporary/virtual definition…
I'm not following you.


Yeah, sorry... here's the full 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>

/* Program to simulate flowcharts, adding console applications.
   Flowcharts are composed of cells, which are interconnected.
   Cells are either linked to other Cells by yes or no, or they
   do not have links. The former are called LinkCell, the later
   are PassCell. PassCells display a message, then the program
   either 'passes' to another Cell or exists.*/

using namespace std;

// Generic Cell class
class Cell {
   protected:
        string msg;
   public:
        string getMsg() { return msg; }
        virtual void flow() = 0;
   };

// Linked Cell class
class LinkCell : public Cell {
   private:
	string val;
	Cell* yLink;
	Cell* nLink;
   public:
	LinkCell( const string&, Cell*, Cell* );	
	void setVal();
	string getVal(){ return val; }
	void flow();
};

LinkCell::LinkCell ( const string& setMsg, Cell *yParam, Cell *nParam ) {
    this->msg = setMsg;
    this->yLink = yParam;
    this->nLink = nParam;
}

void LinkCell::setVal(){
    cout << this->getMsg() << " [y/n] ";
    cin >> this->val;
    while (( this->getVal()!="y" ) && ( this->getVal()!="n" )){
            cout << "Please enter y or n: ";
            cin >> val;
    }
}

void LinkCell::flow(){
	setVal();
    if ( this->getVal()=="y" ) { this->yLink->flow(); }
    if ( this->getVal()=="n" ) { this->nLink->flow(); }
}

// EndCell class
class EndCell : public Cell {
   public:
	EndCell( const string& );	
	void flow();
};

EndCell::EndCell ( const string& setMsg ) { 
	this->msg = setMsg;
}

void EndCell::flow() {
	cout << this->getMsg() << endl;
}

// PassCell class
class PassCell : public Cell {
	Cell *link;
   public:
	PassCell( const string&, Cell* );
	void flow();
};

PassCell::PassCell ( const string& setMsg, Cell *param ) {
	this->msg = setMsg;
	this->link = param;
}

void PassCell::flow() {
	cout << this->getMsg();
	this->link->flow();
}


int main() {

	PassCell wait_year( "Wait another year", &over_eighteen );
        LinkCell over_eighteen( "Are they both over 18", &next_step, &wait_year;)

        // ignore that next_step is not defined

	over_eighteen.flow();

	return 0;
}
First, I can't see why, in your design, you would link two Cells recursively. That said, the problem is you need a way to set the links after the creation of the objects. You could do this:

1
2
3
void PassCell::setLink(Cell * param) {
	this->link = param;
}
Oh... thanks! I somehow overlooked that possibility.
really why so complicated design .. as you are using composition and agregation .. i have a question that can this two be used simultanously ?
Topic archived. No new replies allowed.