Graph implementation with element of a class

Dec 27, 2019 at 12:18am
Is it possible to implement a graph with object of a class?
I mean, I made a class and it has many inherited classes, that have 2 pointers and another value. These 2 pointers have to point to the values of two different object of inherited classes.
If I don't explain well, I can try to be more precise..
Dec 27, 2019 at 2:06pm
I'm not entirely sure what you're asking. Could you post a representative example of what you have?

To represent a general graph you need an object that is capable of pointing to an arbitrary number of other objects. A common way of doing this is by storing pointers in a vector. For example,
1
2
3
class Node{
    std::vector<Node *> edges;
};
Dec 28, 2019 at 3:51pm
I have to make a class Circuit, composed by logic port of a class called Port. Each element of Port has two pointers (I made pointers to bool but I don't know if it's better to point to other element of class Port) and a value that can be 0,1 or a character.
Dec 28, 2019 at 8:03pm
So, like this?

1
2
3
4
5
6
class ElectronicComponent{
    ElectronicComponent *first, *second;
public:
    virtual ~ElectronicComponent();
    virtual bool get_state() = 0;
};
Last edited on Dec 28, 2019 at 8:03pm
Dec 29, 2019 at 11:45am
Yes, but I have two bool pointers..
I don't know which is better, because first elements have to point to bool values, given in an input vector, then other elements can point to other ElectronicComponent.
Dec 29, 2019 at 2:26pm
You could do it like helios suggests. To handle the inputs, you derive a new ElectronicComponent whose output is constant:
1
2
3
4
5
6
7
8
class ConstantComponent  : public ElectronicComponent {
    bool state;
public:
    ConstantComponent(bool s) : 
        ElectronicComponent(nullptr, nullptr),
        state(s);
    virtual bool get_state() { return state; };
};


One of these corresponds to each of your inputs.

VERY IMPORTANT!!

You need to determine if your circuit can have feedback. In other words, can there be a cycle in the circuit, such as with a flip-flop or amplifier, or are you guaranteed that the output of each component can never affect that component's input?

If the circuit can contain cycles then you need to add some logic to prevent recursion in your get_state() method.
Dec 29, 2019 at 2:52pm
You need to determine if your circuit can have feedback. In other words, can there be a cycle in the circuit, such as with a flip-flop or amplifier, or are you guaranteed that the output of each component can never affect that component's input?

If the circuit can contain cycles then you need to add some logic to prevent recursion in your get_state() method.
In this case, you will almost certainly also need an update_state() function. update_state() will set the state of the component by calling first and second's get_state() and simulating the behavior of the component. For example, AND would do state = first->get_state() && second->get_state(). Meanwhile, get_state() will simply return the value of state.
This would not only solve the problem of infinite recursion, but it would also simulate finite-speed propagation of signals inside the circuit, without which it's impossible to correctly simulate circuits such as flip-flops.
Last edited on Dec 29, 2019 at 4:44pm
Dec 29, 2019 at 9:31pm
I have to check if there is a feedback, bacause I have to make (also) only combinational circuits.
But sorry, I don't understand what are you saying about this function
Dec 29, 2019 at 9:54pm
Let's cross that bridge when we get to it. Check if feedback must be supported.
Dec 29, 2019 at 10:19pm
Modifying the class I had this error :
1
2
3
4
5
6
7
8
9
10
11
12
13
class myClass{
public:
     myClass();
    ~myClass();
     void setInput(const myClass&);
protected:
     myClass *_x1;
private:
};

void setInput(const myClass& a){
    _x1=a;
}


cannot convert 'const myClass' to 'myClass*' in assignment
_x1 = a;

Why this?
Last edited on Dec 29, 2019 at 10:19pm
Dec 30, 2019 at 12:53am
1
2
double *ptr;
ptr = 3.141592;

¿do you understand the error there?
Dec 30, 2019 at 11:40am
yes, but if I pass the address by operator &,
I don't need to use the pointer, right?
Dec 30, 2019 at 12:30pm
No, you're passing a reference to a myClass object, not a pointer:
1
2
void setInput(const myClass &a); // a is a reference to a myClass object
void setInput(const myClasss *a);  // a is a pointer to a myClass object 


Don't think if references and pointers as the same thing. Think of a reference as a synonym for another object. Since it's an object and not a pointer, you need to set _x1 to it's address:
1
2
3
void setInput(const myClass& a){
    _x1 = &a;
}

I know that the syntax is confusing. Sometimes & means "reference to" and sometimes it means "address of" and sometimes it even means "bit-wise and".
Dec 30, 2019 at 4:19pm
now, for the next error
_x1 = &a;
error: assigning to 'myClass *' from incompatible type 'const myClass *'


1
2
3
const int answer = 54;
int *ptr = &answer;
*ptr = 42;



also, const myClass& a may bind to a temporary, ¿how could you avoid for `_x1' to end up pointing to garbage?
Topic archived. No new replies allowed.